37 Character Stream - Byte Buffered Input Stream: BufferedInputStream

Knowledge points:

java.io.BufferedInputStream extends InputStream

Byte buffer input stream:
inherits the method of the parent class:
 public void close() closes this input stream and releases any system resources associated with this stream
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

Constructor:
   BufferedInputStream(InputStream in) Creates a BufferedInputStream and saves its parameters, the input stream in, so that In the future use
   BufferedInputStream(InputStream in,int size) to create a BufferedInputStream with buffer size, and save its parameters, the input stream in, for future use of
   parameters:
     InputStream in: byte input stream We can pass FileInputStream to increase a buffer, increase The reading efficiency of FileInputStream
     int size: specify the size of the buffer stream, not specifying is the default

     step:
     1. Create a FileInputStream object, and bind the data source to be read in the construction method
     2. Create a BufferedInputStream object and pass it in the construction method FileInputStream object, improve the reading efficiency of FileInputStream object
     3. Use the read object in the BufferedInputStream object to read the file
     4. Release resources

Code:

package demo38字节缓冲输入流BufferedInputStream;

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

/*
java.io.BufferedInputStream extends InputStream
字节缓冲输入流:
继承父类的方法:
 public void close() 关闭此输入流并释放与此流相关联的任何系统资源
public abstract int read()从输入流读取数据的下一个字节
public int read(byte[] b)从输入流中读取一些字节,并将他们存储到字节数组b中

构造方法:
   BufferedInputStream(InputStream in) 创建一个BufferedInputStream 并保存其参数,即输入流in,以便将来使用
   BufferedInputStream(InputStream in,int size) 创建具有缓冲区大小的 BufferedInputStream,并保存其参数,即输入流in,以便将来使用
   参数:
     InputStream in:字节输入流 我们可以传递FileInputStream增加一个缓冲区,提高FileInputStream的读取效率
     int size:指定缓冲流 内部的大小,不指定是默认的

     步骤:
     1.创建FileInputStream对象,构造方法中绑定要读取的数据源
     2.创建BufferedInputStream的对象,构造方法中传递FileInputStreanm对象,提高FileInputStream对象的读取效率
     3.使用BufferedInputStream对象中的read对象,读取文件
     4.释放资源
 */
public class DemoBuffereInputStream {
    public static void main(String[] args) throws IOException {
        //1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        FileInputStream fis=new FileInputStream("E:\\多线程\\aa.txt");
        //2.创建BufferedInputStream的对象,构造方法中传递FileInputStreanm对象,提高FileInputStream对象的读取效率
        BufferedInputStream bis=new BufferedInputStream(fis);
        //3.使用BufferedInputStream对象中的read对象,读取文件
        //int read()从输入流中读取数据下一个字节

        /*int len=0;//记录读取到的字节
        while ((len=bis.read())!=-1){
            System.out.println(len);
        }*/

        //public int read(byte[] b)从输入流中读取一些字节,并将他们存储到字节数组b中
        byte[] bytes=new byte[1024];//存储每次读取的数据
        int len=0;//记录每次读取的有效字节个数
        while((len=bis.read(bytes))!=-1){
            // 2.String(byte[] bytes,int offset,int length):把字节数组的一部分转换为字符串 offset数组的开始索引 length:转换字符串的个数
            System.out.println(new String (bytes,0,len));
        }


        //4.释放资源
        bis.close();
    }
}

run:

 

Guess you like

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