Buffered buffer of io stream

Introduction:

When BufferReader reads text, it will read character data from the file to the buffer. If you continue to use the read() method, it will read data from the buffer, reducing the process of connection encoding and transmission, and improving efficiency. Only the buffer When the data is full, it will be written again, because when we use the Reader method to read the file, we will read binary or octal bytes, it is difficult to see the content, so we need to convert it to text , in addition to the char variable, there is also a Buffered buffer, we can store the received data in the buffer, the buffer will convert it into text, and then output it when the buffer data is full

Declaration method:

Reader fr = new FileReader("D:\\java production\\advanced features\\Osmanthus fragrans.txt");//Set the reading path
BufferedReader br = new BufferedReader(fr);//Read the object into the buffer

The code is implemented as follows:

package com.ytzl.第二章.demo4.io流.two;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;

public class BufferReaderTest {
    public static void main(String[] args) throws Exception{
        /*BufferReader在读取文本的时候,会从文件中读取字符数据
        * 到缓冲区,如果继续使用read()方法会从缓冲区读取数据,
        * 减少了连接编码传输的过程,提高了效率,只有缓冲区数据
        * 满了,才会再次进行写入
        * */
        //优化创建对象的方式
        Reader fr = new FileReader("D:\\java制作\\高级特性\\桂花宝典.txt");//设置读取路径
        BufferedReader br = new BufferedReader(fr);//读取对象放入缓冲区
        //readLine()每次读取一行数据
        //String line = br.readLine();
        //System.out.println(line);
        //循环读取
        System.out.println(fr.read());
        String line=null;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
        fr.close();
    }
}

Guess you like

Origin blog.csdn.net/ypf3442354429/article/details/124770581