32 character input stream Reader

When using byte streams to read text files, there may be a small problem, that is, when encountering Chinese characters, the complete characters may not be displayed, that is because a Chinese character may occupy multiple bytes of storage, so java provides some character stream classes

Read Chinese using byte stream:

  GBK: One Chinese two bytes

  UTF-8: One Chinese three bytes

You can use the character stream to read characters, whether it is Chinese, English, numbers or symbols, you can read and write

Byte streams read one byte at a time, character streams read one character at a time

 

Knowledge points:

java.io.Reader: character input stream, the top-level parent class of character input stream, defines some common member methods, and is a
  public member method of an abstract class:
    int read() reads a single character
    int read(char[ ] cbuf) Read multiple characters in turn, write the characters into the array
    void close() Close the stream and release all resources associated with it The
    abstract class cannot be used, we have to use its subclasses, BuffereReader, CharArrayReader, FileterReader, InputStreamReader , PipedReader, StringReader

    We learn FileReader - inherits InputStreamReader - inherits Reader
    FileReader: file character input stream function: read the data in the hard disk file into memory in the form of characters
    Construction method:
      FileReader(String filename) String filename file path
      FileReader(File file)

   The steps of using a file character stream in File file:
      1. Create a FileReader object and bind the read data in the construction method
      2. Use the method reader method in the FileReader object to read the data
      3. Release resources

Code:

package demo32Reader;

import java.io.FileReader;
import java.io.IOException;

/*
java.io.Reader:字符输入流,是字符输入流的最顶层父类,定义了一些共性的成员方法,是一个抽象类
  公共的成员方法:
    int read() 读取单个字符
    int read(char[] cbuf) 依次读取多个字符,将字符写入数组
    void close() 关闭该流并释放与之关联的所有资源
    抽象类无法使用,我们得使用他的子类,BuffereReader,CharArrayReader,FileterReader,InputStreamReader,PipedReader,StringReader

    我们学习FileReader-继承了InputStreamReader-继承了Reader
    FileReader:文件字符输入流 作用:把硬盘文件中的数据以字符的方式读取到内存中
    构造方法:
      FileReader(String filename)  String filename文件路径
      FileReader(File file)  File file一个文件

   字符流的使用步骤:
      1.创建一个FileReader对象,构造方法中绑定读取的数据
      2.使用FileReader对象中的方法reader方法读取数据
      3.释放资源
 */
public class DemoReader {
    public static void main(String[] args) throws IOException {
        //1.创建一个FileReader对象,构造方法中绑定读取的数据
        FileReader fr=new FileReader("E:\\多线程\\c.txt");
        //2.使用FileReader对象中的方法reader方法读取数据
        /*int len=0;
        while((len=fr.read())!=-1){
            System.out.print((char) len);
        }*/
        //int read(char[] cbuf) 依次读取多个字符,将字符写入数组
        char[] cs=new char[1024];//存储读取到的多个字符
        int len=0;//记录读取到的有效字符个数
        while((len=fr.read())!=-1){
            /*String类的构造方法
              String(char[] value) 把字符数组转换为字符串
               String(char[] value,int off,int count) 把字符数组转换为字符串 offet数组的开始索引 count转换字符的个数
            */
            System.out.println(new String(cs,0,len));
        }


        //3.释放资源
        fr.close();
    }
}

result:

Guess you like

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