Java基础之(IO流)

  

简介:

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

一、File类

  java.io.File类:文件和目录路径名的抽象表示形式,与平台无关 File ,能新建、删除、重命名文件和目录。但 File 不能访问文件内容本身,如果需要访问文件内容本身,则需要使用输入/输出流。

  File对象可以作为参数传递给流的构造函数

  File类的常见构造方法:

  1、public File(String pathname) 以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。

  2、public File(String parent,String child) 以parent为父路径,child为子路径创建File对象。

package main.dyh;


import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class TestFile {

    //文件夹操作
    @Test
    public void fun1(){
        File file1 = new File("D:\\aa");
        boolean mkdir = file1.mkdir();//创建文件夹,D盘之前是没有文件夹aa的
        File file2 = new File("D:\\aa","bb");
        boolean mkdirs = file2.mkdirs();//创建文件夹,并在文件夹中创建子文件夹bb

        File file3 = new File("D:\\");
        String[] list = file3.list();//查询D盘中所有目录和文件
        for (String s : list) {
            System.out.println(s);
        }

        File file4 = new File("D:\\");
        File[] files = file4.listFiles();//查询D盘中所有文件和文件夹,并显示路径
        for (File file : files) {
            System.out.println(file);
        }
    }

    //文件操作
    @Test
    public void fun2() throws IOException {
        File file1 = new File("D:\\a.txt");
        boolean newFile = file1.createNewFile();//创建文件,注意的是,如果把该文件建在一个文件夹中,该文件夹必须存在,或者报异常,如果之前有该文件,会覆盖

        //-------------------------------
        File file2 = new File("D:\\a.txt");
        String name = file2.getName();//获得该文件的完整名称,包括后缀
        System.out.println(name);

        File file4 = new File("./a.txt");
        File absoluteFile = file4.getAbsoluteFile();
        System.out.println(absoluteFile);

//     -------------------------------------------
        File filep = new File(".\\test1.txt");
        File filea = new File("D:\\workspace\\test\\test1.txt");
        System.out.println("-----默认相对路径:取得路径不同------");
        System.out.println(filep.getPath());
        System.out.println(filep.getAbsolutePath());
        System.out.println("-----默认绝对路径:取得路径相同------");
        System.out.println(filea.getPath());
        System.out.println(filea.getAbsolutePath());




        File file6 = new File("D:\\a\\a.txt");
        String parent = file6.getParent();//得到父目录
        System.out.println(parent);

        File file3 = new File("D:\\a\\a.txt");
        boolean nfile = file3.renameTo(new File("D:\\a\\b.txt"));

        File file7 = new File("D:\\b.txt");
        boolean newFile1 = file7.createNewFile();
        boolean delete = file7.delete();//删除指定文件
    }

    /**
     * 文件检测
     */
    @Test
    public void fun3() {
        File file1 = new File("D:\\a.txt");
        boolean exists = file1.exists();//文件是否存在
        System.out.println(exists);

        boolean bread = file1.canRead();//文件是否可读
        System.out.println(bread);
        boolean bwrite = file1.canWrite();//文件是否可写
        System.out.println(bwrite);

        boolean file = file1.isFile();
        System.out.println(file);
        boolean directory = file1.isDirectory();//是否是文件夹
        System.out.println(directory);

        long length = file1.length();
        System.out.println(length);

        long l = file1.lastModified();//返回此抽象路径名表示的文件最后一次被修改的时间
        System.out.println(l);
    }
}
View Code

二、什么是IO流

  IO流用来处理设备之间的数据传输。 Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。

  java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

  1、输入流与输出流概念

  输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。

  输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

  2、流的分类

  按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)。

  按数据流的流向不同分为:输入流,输出流。

  按流的角色的不同分为:节点流,处理流。

  Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

 

   处理流与节点流的区别:

  节点流:节点流可以从一个特定的数据源读写数据。

  处理流:处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

  3、IO流的体系

三、IO流的读写操作

  1、InputStream 和 Reader 是所有输入流的基类。

  InputStream(典型实现:FileInputStream)

  int read()

  int read(byte[] b)

  int read(byte[] b, int off, int len)

  Reader(典型实现:FileReader)

  int read() int read(char [] c)

  int read(char [] c, int off, int len)

  程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。

package main.dyh;

import org.junit.Test;

import java.io.*;

public class TestOutIn {

    @Test
    public void fun1() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(new File("D:\\a.txt"));
            char[] c = new char[1024];
            int len = 0;
            while ((len = fileReader.read(c)) != -1){
                System.out.println(new String(c,0,len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void fun2() {
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(new File("D:\\a.txt"));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fs.read(b)) != -1){
                System.out.println(new String(b,0,len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
View Code

  2、OutputStream 和 Writer 也非常相似: (典型实现:FileOutputStream)

  void write(int b/int c);

  void write(byte[] b/char[] cbuf);

  void write(byte[] b/char[] buff, int off, int len);

  void flush();

  void close();

  需要先刷新,再关闭此流,因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数 :

  void write(String str);

  void write(String str, int off, int len);

package main.dyh;

import org.junit.Test;

import java.io.*;

public class TestInput {

    @Test
    public void fun1(){
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(new File("D:\\a.txt"));
            fileWriter.write("HelloWorld!!");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void fun2(){
        FileOutputStream fo = null;
        try {
            fo = new FileOutputStream(new File("D:\\a.txt"));
            fo.write("woaibeijingtiananmen".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
View Code

四、处理流

  1、缓冲流

  为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。

  根据数据操作单位可以把缓冲流分为:

  BufferedInputStream 和 BufferedOutputStream

  BufferedReader 和 BufferedWriter

  缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法 对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出。

package main.dyh;

import org.junit.Test;

import java.io.*;

public class TestBuffer {

    @Test
    public void fun1(){
        BufferedOutputStream bo = null;
        BufferedInputStream bi = null;
        try {
            bo = new BufferedOutputStream(new FileOutputStream("D:\\d.txt"));
            bi = new BufferedInputStream(new FileInputStream("D:\\d.txt"));

           bo.write("zhonghuarenming".getBytes());//把数据写到硬盘上

           byte[] b = new byte[1024];
           int len = 0;
           while ((len = bi.read(b)) != -1){//把数据读到内存中
               System.out.println(new String(b,0,len));
           }
           bo.flush();//刷新缓冲区
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bi.close();
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/soft2018/p/10317148.html