Introduction and summary of Java input and output related concepts (File, IO stream) (11) (FileReader)

Get into the habit of writing together! This is the 11th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

The last article introduces and summarizes the concepts related to Java input and output (File, IO stream) (10) The concept of character input stream Reader is roughly introduced. In this article, we will give a detailed explanation of the subclass of Reader: FileReader, including concept and usage.

FileReader

Concept of FileReader

FileReader is the most commonly used Reader implementation class, which is very similar to FileInputStream, except that FileReader reads in characters; FileInputStream reads in bytes.

So analogous to the FileInputStream mentioned earlier, we can infer that FileReader is used to read text content. Depending on the encoding scheme, a character may be equivalent to one or more bytes. For example, a Chinese character may be represented by two bytes or three bytes in different encodings.

Fundamentals of FileReader

  • When using FileReader object operations to read and input files, JVM first reads the text, then converts its format to Unicode encoding format, and then operates; when using FileWriter for text file output, it converts the Unicode-encoded data into The encoding format of the local host (such as ASCII or GBK), and then output.

  • For FileReader and FileWriter, in fact, the operation methods of the two classes are basically the same as those of FileInputStream and FileOutputStream. As mentioned above, the former is based on characters and takes characters as the unit; the latter is based on bytes and takes bytes as the basic operation. unit.

If the operating files are not text files (text files such as txt, non-text files such as music and pictures), then we'd better use FileInputStream and FileOutputStream for file input and output. Because it is passed around in units of characters, a careless code will be garbled, or the file will be damaged.

Method introduction of FileReader

Construction method

// 已存在File 对象,根据此File 对象创建一个FileReader
FileReader(File file)

// 已存在FileDescriptor 的情况下创建一个FileReader
FileReader(FileDescriptor fd)

// 给定文件名,根据这个文件名创建FileReader
FileReader(String fileName)
复制代码

Common method

// 关闭流,释放资源
void close()  

// 这个就和“字符”的概念有关了,作用是返回这个这个流使用的字符编码的名称
String getEncoding()   

// 读取单个字符(注意:返回的值为读入字符对应的int值,范围:0~65535)
int read()

// 按照参数,将字符读入数组中,存在于数组的某一部分。返回值是读入字符的个数
int read(char[] cbuf, int offset, int length)

// 用来判断这个流是否已经准备好,是否可以用于读取。返回值为读入字符的个数
boolean ready()
复制代码

Example of use

Use the FileWriter class to write a string of characters to the file, and then use the FileReader to read the written content.

public static void main(String[] args) throws Exception {
  FileWriter fileWriter = new FileWriter("copy.txt");
  // 我们可以回忆一下,在使用FileOutputStream 的时候,可以用个字节数组作为"中间层"
  // 而使用FileWriter 的时候可以在此可以直接写入字符串,不用转化为字节数组
  fileWriter.write ("12345");
  fileWriter.close();

  char[] chars = new char[16];
  // 读取copy.txt 文件
  FileReader in = new FileReader("copy.txt");
  int len = in.read(chars);
  System.out.println(new String(chars,0,16));
  in.close();
}
复制代码

Summarize

In this article, we introduced the use of FileReader. We can indeed see that the use is similar to FileInputStream, the difference is that FileReader is based on characters, while FileInputStream is based on bytes.

In the next article we continue to introduce other subclasses of the Reader class.

Guess you like

Origin juejin.im/post/7085026614525820958