30 "byte input stream reading method"

Knowledge points:

java.io.InputStream: Byte input stream, this abstract class represents the superclass of all classes of byte input stream
Define a common method for subclasses:
public void close() Close this input stream and release the associated stream any system resource
public abstract int read() reads the next byte of data from the input stream
public int read(byte[] b) reads some bytes from the input stream and stores them in byte array b

FileInputStream (): File byte input stream
Function: Read the data of the hard disk file into the memory for use
Constructor:
  FileInputStream (String name)
  FileInputStream (File file)
 Parameter: read the data source of the
      file String name: the path of the file
      File file: file
public abstract int read() reads the next byte of data from the input stream
public abstract int read() reads the next byte of data from the input stream
public abstract int read() reads the data from the input stream Next byte, read a byte in the file and return, read to the end of the file and return -1

The function of the construction method:
1. A FileInputStream object will be created
2. The construction method specified by the FileInputStream object will be read Steps to fetch the file

bytes input stream:
1. Create a FileInputStream object, and bind the data source to be read in the construction method
2. Use the method read in the FileInputStream object to read the file
3. Release resources

Example 1:

package demo30字节输入流读取方法;
/*
java.io.InputStream:字节输入流,此抽象类表示字节输入流的所有类的超类
定一个子类的共性方法:
public void close() 关闭此输入流并释放与此流相关联的任何系统资源
public abstract int read()从输入流读取数据的下一个字节
public int read(byte[] b)从输入流中读取一些字节,并将他们存储到字节数组b中

FileInputStream():文件字节输入流
作用:把硬盘文件的数据,读取到内存中使用
构造方法:
  FileInputStream(String name)
  FileInputStream(File file)
 参数:读取文件的数据源
      String name:文件的路径
      File file:文件
public abstract int read()从输入流读取数据的下一个字节
public abstract int read()从输入流读取数据的下一个字节
public abstract int read()从输入流读取数据的下一个字节,读取文件中的一个字节并返回,读取到文件的末尾返回-1

构造方法的作用:
1.	会创建一个FileInputStream对象
2.	会把FileInputStream对象指定的构造方法中要读取的文件

字节输入流的步骤:
1.创建一个FileInputStream对象,构造方法中绑定要读取的数据源
2.使用FileInputStream对象中的方法read,读取文件
3.释放资源
 */

import java.io.FileInputStream;
import java.io.IOException;

public class DenoInputStream {
    public static void main(String[] args)  throws IOException {
       //1.创建一个FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis=new FileInputStream("E:\\多线程\\a.txt");

        //2.使用FileInputStream对象中的方法read,读取文件
        //public abstract int read()从输入流读取数据的下一个字节
        //public abstract int read()从输入流读取数据的下一个字节,读取文件中的一个字节并返回,读取到文件的末尾返回-1

        /*依次读取
        int len=fis.read();
        System.out.println(len);// 97 a

        len=fis.read();
        System.out.println(len);//98 b
        len=fis.read();
        System.out.println(len);//99 c

        len=fis.read();
        System.out.println(len);//-1*/

        //以上代码读取是一个重复的过程,所以使用循环优化 不知道文件有多少字节,使用while循环,while结束条件,读取到-1结束
        /*
           布尔表达式(len=fis.read())!=-1
             1 fis.read();读取一个字节
             2 len=fis.read();把字节赋值给len
             3 (len=fis.read())!=-1判断变量len是否等于-1
         */
        int len=0;//记录读取到的字节
        while((len=fis.read())!=-1){
            System.out.println((char)len);//转换为字符abc
        }


        //3释放资源
        fis.close();

    }
}

result:

 

Example 2

Knowledge points:

The method of reading multiple bytes at a time from the byte input stream:
   int read(byte[] b) reads a certain byte of data from the input stream at a time and stores it in the buffer array b

Constructor of the String class :
 1.String(byte[] bytes): Convert byte array to string
 2.String(byte[] bytes,int offset,int length): Convert part of byte array to the start index of string offset array length: the number of converted strings

package demo30字节输入流读取方法;

import java.io.FileInputStream;
import java.io.IOException;

/*
字节输入流一次读取多个字节的方法:
   int read(byte[] b)从输入流中一次读取一定数据的字节,并将其存储在缓冲区数组b中

String类的构造方法:
 1.String(byte[] bytes):把字节数组转换为字符串
 2.String(byte[] bytes,int offset,int length):把字节数组的一部分转换为字符串 offset数组的开始索引 length:转换字符串的个数
 */
public class demoInputStream02 {
    public static void main(String[] args) throws IOException {
       //创建FileInputStream对象,构造方法绑定哟啊读取的数据源
        //初始b.txt的内容为ABCDE
        FileInputStream fis=new FileInputStream("E:\\多线程\\b.txt");//创建一个流对象,指向我们要读取的数组
        //使用FileInputStream对象中的方法read读取文件
      /* byte[] bytes=new byte[2];//创建一个byte的数组,存储2个字节
        //数组起到缓冲作用,存储每次读取的字符串,设置数组多大就可以读取几个字节,把2改为5,就可以一次把b.txt文件中的ABCDE一次读取出来
        int len=fis.read(bytes);// 2   方法返回值为每次读取有效字节的个数
        System.out.println(len);
        //System.out.println(Arrays.toString(bytes)); // 65 66
        System.out.println(new String(bytes));//A B

        len=fis.read(bytes);// 2   方法返回值为每次读取有效字节的个数
        System.out.println(len);//2
        System.out.println(new String(bytes));//C D 再一次读取,读取到CD,覆盖数组中的AB

        len=fis.read(bytes);// 2   方法返回值为每次读取有效字节的个数
        System.out.println(len);// 1 读取到b.txt中最后一个字母返回,读取的个数为1
        System.out.println(new String(bytes)); //E D  再一次读取读取到E,覆盖数组中的C ,E D


        System.out.println("==================================");*/
/*
发现上面读取是一个重复的过程,可以使用while循环优化
 */
        byte[] bytess=new byte[1024];//存储读取到的多个字节
        int len1=0;//记录每次读取到的字节
        while((len1=fis.read(bytess))!=-1){
            //System.out.println(bytes);//输出字符串地址
            //2.String(byte[] bytes,int offset,int length):把字节数组的一部分转换为字符串 offset数组的开始索引 length:转换字符串的个数
            System.out.println(new String(bytess,0,len1));//调用上面字符串的第二个方法,因为byte数组1024,读取ABCDE还会空许多个字节,调用第二个方法,只把读取到的字节转换为字符串

        }
        //释放资源
        fis.close();
    }

}

result:

 The result in the comment: byte[2] reads 2 bytes at a time

 

while loop optimization result:

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123996174