IO流之字节输入流【InputStream】(二)

287 阅读4分钟

这是我参与更文挑战的第 9 天,活动详情查看: 更文挑战

日积月累,水滴石穿 😄

1、字节输入流【InputStream】

java.io.InputStream此抽象类是表示输入字节流的所有类的超类。可以读取字节信息到内存中。它定义了字节输出流的基本公共方法。

  • public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
  • public abstract int read() : 从输入流读取数据的下一个字节
  • public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b 中

1.1 FileInputStream 【文件字节输入流】

把磁盘文件中的数据读取到内存中。

1.1.1 构造方法

image.png 我们常用的也就这两个:

  • FileInputStream(File file) :通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
  • FileInputStream(String name) :通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。

参数:读取文件的数据源

构造方法的作用:

  • 1、创建一个FileInputStream对象
  • 2、会把FileInputStream对象指向构造方法中要读取的文件 构造举例,代码如下:
public static void main(String[] args) {
     // 使用File对象创建流对象 
     File file = new File("a.txt"); 
     FileInputStream fos = new FileInputStream(file); 
     // 使用文件名称创建流对象 
     FileInputStream fos2 = new FileInputStream("b.txt"); 
}

当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出 FileNotFoundException

1.1.2 读取字节数据

  • 一次读取一个字节:read(),每次可以读取一个字节的数据,读取到文件末尾,返回 -1
public static void main(String[] args)throws Exception{
    //创建FileInputStream对象
    //a.txt中的内容为:100
    FileInputStream fis = new FileInputStream("IO流//a.txt");
    //使用read()方法读取
    //从此输入流中读取一个数据字节。读取到文件末尾返回-1
    int read = fis.read();
    System.out.println(read);  //49
    int read2 = fis.read();
    System.out.println(read2);//48
    int read3 = fis.read();
    System.out.println(read3);//48
    int read4 = fis.read();
    System.out.println(read4);//-1
    //释放资源
    fis.close();
}

image.png

以上代码是一个重复的工程,进行优化

public static void main(String[] args)throws Exception{
    //创建FileInputStream对象
    FileInputStream fis = new FileInputStream("IO流//a.txt");
    //使用read()方法读取
    //从此输入流中读取一个数据字节。读取到文件末尾返回-1
    //使用while循环,结束条件为:读取到-1时结束
    int len = 0;  //记录读取到的字节
    while ( (len = fis.read() ) != -1){
        System.out.println(len);
    }
    //释放资源
    fis.close();
}

image.png 读出来的为什么是49、48、48? 这是因为在读取文件的时候,会去查询编码表,0-127查询 ASCII 表,1 的 ASCII 是 49。0 的 ASCII 是 48。要怎么才能变为1、0、0呢?很简单转换一下就行: System.out.print((char)len); image.png

  • 一次读取多个字节:read(byte[] b) ,每次读取 b 的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回 -1

a.txt内容修改为ABCDE

public static void main(String[] args) throws Exception{
    //创建FileInputStream对象
    FileInputStream fis = new FileInputStream("IO流//a.txt");
    byte[] bytes = new byte[2];
    //read(byte[] b)
    // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
    int read = fis.read(bytes);
    System.out.println(read);  //2  读取的有效字节个数
    System.out.println(Arrays.toString(bytes));  //[65, 66]

    int read2 = fis.read(bytes);
    System.out.println(read2); //2
    System.out.println(Arrays.toString(bytes)); //[67, 68]

    int read3 = fis.read(bytes);
    System.out.println(read3); //1
    System.out.println(Arrays.toString(bytes)); //[69, 68]

    int read4 = fis.read(bytes);
    System.out.println(read4); //-1
    fis.close();
}

image.png

上述得出的结果看起来有点不舒服,将字节转换为字符串。使用 String 类中 String(byte[] bytes) 构造方法。

String(byte[] bytes):把字节数组转换为字符串

public static void main(String[] args) throws Exception{
   //创建FileInputStream对象
    FileInputStream fis = new FileInputStream("IO流//a.txt");
    byte[] bytes = new byte[2];
    //使用read()方法读取
    //read(byte[] b)
    // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
    int read = fis.read(bytes);
    System.out.println(read);  //2    读取的有效字节个数
    System.out.println(Arrays.toString(bytes));  //[65, 66]
    System.out.println(new String(bytes));  //AB

    int read2 = fis.read(bytes);
    System.out.println(read2); //2
    System.out.println(Arrays.toString(bytes)); //[67, 68]
    System.out.println(new String(bytes));  //CD

    int read3 = fis.read(bytes);
    System.out.println(read3); //1
    System.out.println(Arrays.toString(bytes)); //[69, 68]
    System.out.println(new String(bytes));  //ED

    int read4 = fis.read(bytes);
    System.out.println(read4); //-1
    fis.close();
}

image.png

可以看到第三次读取,有个错误数据 D。这是为什么呢?这是由于我们每次读取都是放入到 bytes里的,每次读取两个,后一次的读取数据将前一次的数据进行覆盖,由于最后一次读取时,只读取一个字节 E ,没有将上次读取的数据没有被完全替换,显示出来为 ED。那要怎么解决呢?

1.1.3 优化

使用 String 类中的另外一个构造方法

String(byte[] bytes, int offset, int length):把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的个数

public static void main(String[] args) throws Exception{
    //创建FileInputStream对象
    FileInputStream fis = new FileInputStream("IO流//a.txt");
    byte[] bytes = new byte[2];
    //使用read()方法读取
    //read(byte[] b)
    // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
    int len = 0;
    while( (len = fis.read(bytes))  != -1){
       System.out.println(new String(bytes,0,len));
    }
    fis.close();
}

image.png

使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率。

  • 如你对本文有疑问或本文有错误之处,欢迎评论留言指出。如觉得本文对你有所帮助,欢迎点赞和关注。