Character input stream [Reader] Read character data

1. Character stream

When reading a text file using a byte stream, when Chinese characters are encountered, the complete characters may not be displayed, because a Chinese character may occupy multiple bytes of storage. So Java provides some character stream classes, which read and write data in units of characters, specifically for processing text files.

1个中文
    GBK:占用两个字节
    UTF-8:占用3个字节		    

1.1 Character input stream [Reader]

java.io.Reader: Character input stream, the top-most parent class of character input stream, defines some common member methods, and is an abstract class

共性的成员方法:
    int read() 读取单个字符并返回
    int read(char[] cbuf)一次读取多个字符,将字符读入数组
    void close() 关闭该流并释放与之关联的所有资源

java.io.FileReader extends InputStreamReader extends Reader

1.2 FileReader class

FileReader: file character input stream
Function: read the data in the hard disk file into the memory in the form of characters

1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。
idea中UTF-8
2. 字节缓冲区:一个字节数组,用来临时存储字节数据

1.3 Construction method:

    FileReader(String fileName)
    FileReader(File file)
    参数:读取文件的数据源
        String fileName:文件的路径
        File file:一个文件
    FileReader构造方法的作用:
        1.创建一个FileReader对象
        2.会把FileReader对象指向要读取的文件

The steps to use the character input stream:
1. Create a FileReader object, bind the data source to be read in the construction method
2. Use the method read in the FileReader object to read the file
3. Release resources

Code:

import java.io.FileReader;
import java.io.IOException;
public class Demo02Reader {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //1.创建FileReader对象,构造方法中绑定要读取的数据源
        FileReader fr = new FileReader("day09_IOAndProperties\\b.txt");
        //2.使用FileReader对象中的方法read读取文件
        /*int read = 0;
        while((read = fr.read()) != -1){
            System.out.print((char) read);
        }*/

        //int read(char[] cbuf)一次读取多个字符,将字符读入数组
        char[] cs = new char[1024];
        int len = 0;//记录的是每次读取的有效的字符个数
        while((len = fr.read(cs))!=-1){
    
    
          /*
            String类的构造方法
            String(char[] value) 把字符数组转换为字符串
            String(char[] value,intoffset,int count) 把字符数组的一部分转换为字符串,offset数组的开始索引,count转换的个数
          */
            System.out.println(new String(cs,0,len));
        }
        // 3.释放资源
        fr.close();
    }

Program demonstration:

  1. Read characters: The read method, you can read one character at a time, promote to int type, read to the end of the file, return -1, read in a loop, and demonstrate the result:
    Insert picture description here

  2. Use the character array to read: read(char[] cbuf), each time the length of b is read into the array, the number of valid characters read is returned, when the end is read, -1 is returned, and the code is used to demonstrate :
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/108516618