First, let me introduce the difference between InputStreamReader and FileReader. The specific content is as follows:
InputStreamReader and BufferedReader. The most important class among them is InputStreamReader , which is the bridge for converting bytes into characters. You can re-specify the encoding method in the constructor. If not specified, the default encoding method of the underlying operating system will be adopted, such as GBK, etc.
FileReader and InputStreamReader involve encoding conversion (specify the encoding method or use the default encoding of os), and garbled code may occur on different platforms! FileInputStream is processed in binary mode and will not have garbled code.
FileInputStream byte streams are read byte byte.
FileReader character streams are read as characters one by one.
BufferedReader bufReader = null;InputStreamReader isr = null;FileReader fr = null;try {for(String fileName:fileNames){ Method 1: isr = new InputStreamReader(new FileInputStream("D:/test.txt"), "utf-8"); bufReader = new BufferedReader(isr); Method 2: fr = new FileReader("D:/test.txt"); bufReader = new BufferedReader(fr); while (bufReader.ready()) { // 1. Get each row of data String dataLine = bufReader.readLine(); }}The difference between InputStream and Reader
There are two abstract classes below java.io: InputStream and Reader
InputStream is a superclass that represents all classes of a byte input stream.
Reader is an abstract class for reading character streams
InputStream provides reading of byte streams, not text reading, which is the fundamental difference between it and Reader class.
That is, the char array or String is read using Reader, and the byte array is read using InputStream.
After clarifying the fundamental difference between the two superclasses, let’s look at the use of their subclasses below. Here are only a few of the most commonly used explanations
InputStream
| __FileInputStream
FileInputStream Gets input bytes from a file in the file system.
Summary of construction methods
FileInputStream (File file)
Create a FileInputStream by opening a connection to the actual file, which is specified by the File object file in the file system.
FileInputStream (FileDescriptor fdObj)
Create a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
FileInputStream (String name)
Create a FileInputStream by opening a connection to the actual file, specified by the pathname name in the file system.
Reader
|--BufferedReader
|___InputStreamReader
|__FileReader
BufferedReader: Read text from a character input stream and buffers individual characters, thereby enabling efficient reading of characters, arrays, and lines.
Summary of construction methods
BufferedReader (Reader in)
Creates a buffered character input stream that uses the default size input buffer.
BufferedReader (Reader in, int sz)
Creates a buffered character input stream that uses the specified size input buffer.
BufferedReader (Java Platform SE 6)
The biggest feature of BufferedReader is the buffer setting. Usually every read request made by Reader will result in corresponding read requests for the underlying character or byte stream. If there is no buffering, each call to read() or readLine() will result in bytes being read from the file and converting it into characters and returning it, which is extremely inefficient.
Use the BufferedReader to specify the size of the buffer, or the default size can be used. In most cases, the default value is large enough.
Therefore, it is recommended to wrap all Readers whose read() operations may be expensive (such as FileReader and InputStreamReader) with BufferedReader.
For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));Buffer input to the specified file.
InputStreamReader (Java Platform SE 6)
InputStreamReader is the bridge between byte streams and character streams: it reads bytes using the specified charset and decodes them into characters. The character set it uses can be specified or explicitly given by the name, or it can accept the platform's default character set.
Summary of construction methods
InputStreamReader (InputStream in) Creates an InputStreamReader that uses the default character set. InputStreamReader (InputStream in, Charset cs) Creates an InputStreamReader that uses the given character set. InputStreamReader (InputStream in, CharsetDecoder dec) Creates an InputStreamReader that uses the given character set decoder. InputStreamReader (InputStream in, String charsetName) Creates an InputStreamReader that uses the specified character set.
Each call to a read() method in the InputStreamReader results in one or more bytes being read from the underlying input stream. To enable effective conversion from byte to character, you can read more bytes from the underlying stream in advance, exceeding the bytes required to satisfy the current read operation.
To achieve maximum efficiency, consider wrapping the InputStreamReader inside the BufferedReader. For example:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
The biggest feature of InputStreamReader is that it can refer to the fixed encoding format of the conversion, which is not possible with other classes. It can be seen from the construction method. This is very useful when reading Chinese characters.
FileReader
1) Introduction to FileReader class:
Subclasses of the InputStreamReader class, all methods (read(), etc.) are inherited from the parent class InputStreamReader;
2) Differences from the InputStreamReader class:
Summary of construction methods
FileReader (File file)
Create a new FileReader given a File from which the data is read.
FileReader (FileDescriptor fd)
Create a new FileReader given a FileDescriptor from which data is read.
FileReader (String fileName)
Create a new FileReader given the file name from which the data is read
The main difference between this class and its parent class InputStreamReader is the constructor, and the main difference is the constructor!
From the constructor of InputStreamReader, the parameters are InputStream and encoding method. It can be seen that when you want to specify the encoding method, the InputStreamReader class must be used; and the parameters of the FileReader constructor are the same as FileInputStream, which is a File object or a String representing the path. It can be seen that when you want to read a file based on the File object or String, use FileReader;
I think the function of the FileReader subclass lies in this small division of labor. The main difference between this class and its parent class InputStreamReader is the constructor, and the main difference is the constructor!
From InputStreamReader
In the constructor, the parameters are InputStream and encoding method. It can be seen that when you want to specify the encoding method, the InputStreamReader class must be used; and the parameters of the FileReader constructor are the same as FileInputStream, which is a File object or a String representing the path. It can be seen that when you want to read a file based on the File object or String, use FileReader;
I think the function of the FileReader subclass lies in this small division of labor.
Two connections and differences
(1) Characters and bytes:
The FileInputStream class uses binary input/output, and the I/O speed is fast and efficient, but its read() method reads a byte (binary data), which is very unfavorable for people to read, and it cannot directly operate on characters in the file, such as replacement and search (it must be operated in bytes);
The Reader class makes up for this defect and can input/output in text format, which is very convenient; for example, you can use the while((ch = filereader.read())!=-1 ) loop to read the file; you can use the BufferedReader's readLine() method to read text line by line.
(2) Encoding
InputStreamReader , it is the bridge for converting bytes into characters. You can re-specify the encoding method in the constructor. If not specified, the default encoding method of the underlying operating system will be adopted, such as GBK, etc.
FileReader and InputStreamReader involve encoding conversion (specify the encoding method or use the default encoding of Os), and garbled code may occur on different platforms! FileInputStream is processed in binary mode and will not have garbled code.
Therefore, when specifying the encoding method, the InputStreamReader class must be used, so it is a bridge for converting bytes into characters;
(3) Cache area
The BufferReader class is used to wrap all Readers whose read() operations may be expensive (such as FileReader and InputStreamReader).
(4) Standardized usage
Summarize the above and come up with better standard usage:
1) File file = new File ("hello.txt");
FileInputStream in=new FileInputStream (file);
2) File file = new File ("hello.txt");
FileInputStream in=new FileInputStream (file); InputStreamReader inReader=new InputStreamReader (in,"UTF-8"); BufferedReader bufReader=new BufferedReader(inReader);
3) File file = new File ("hello.txt");
FileReader fileReader=new FileReader(file); BufferedReader bufReader=new BufferedReader(fileReader);