First of all to introduce the difference between InputStreamReader and FileReader, the specific content as follows:
InputStreamReader and BufferedReader. One of the most important classes is InputStreamReader, which is a bridge between bytes converted to characters. You can specify the encoding in the constructor, and the default encoding of the underlying operating system, such as GBK, will be used if unspecified.
FileReader and InputStreamReader involve encoding conversion (specify encoding or adopt OS default encoding), may appear garbled phenomenon on different platform! and FileInputStream in binary way to deal with, will not appear garbled phenomenon.
FileInputStream bytes are read in one byte.
FileReader character streams are read in one character.
BufferedReader bufreader = null;
InputStreamReader ISR = null;
FileReader fr = null;
try {
for (String filename:filenames) {
method one:
ISR = new InputStreamReader (New FileInputStream ("D:\test.txt") ), "Utf-8");
Bufreader = new BufferedReader (ISR);
Method Two:
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 of all classes that represent the byte input stream
Reader is an abstract class for reading character streams
InputStream provides a reading of a byte stream, not a text read, which is fundamentally different from the reader class.
That is, a char array or string is read by reader, and a byte array is read using InputStream.
Figuring out the fundamental differences between the two superclass, and then looking at the use of their subclass below, here are only a few of the most common instructions
InputStream
| __fileinputstream
FileInputStream gets input bytes from a file in the file system.
Construction Method Summary
FileInputStream (File file)
Creates a fileinputstream by opening a connection to the actual file, which is specified by file object filename in the filesystem.
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)
Creates a fileinputstream by opening a connection to the actual file, which is specified by the pathname name in the file system.
Reader
|--bufferedreader
|___inputstreamreader
|__filereader
BufferedReader: Reads text from the character input stream, buffers individual characters, enabling efficient reading of characters, arrays, and rows.
Construction Method Summary
BufferedReader (Reader in)
Creates a buffer character input stream that uses the default size input buffer.
BufferedReader (Reader in, int sz)
Creates a buffer character input stream that uses the specified size input buffer.
BufferedReader (Java Platform SE 6)
The biggest feature of BufferedReader is the setting of the buffer. Usually each read request made by reader will result in a corresponding read request to the underlying character or byte stream, and if there is no buffer, each call to read () or readLine () causes the byte to be read from the file and converted to a character to return, which is extremely inefficient.
You can use BufferedReader to specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.
Therefore, it is recommended to use BufferedReader to wrap all Reader (such as FileReader and InputStreamReader) whose read () operation may be expensive.
For example
BufferedReader in
= new BufferedReader (New FileReader ("foo.in"));
The input of the specified file is buffered.
InputStreamReader is a bridge of byte flow to character streams: it reads bytes using the specified charset and decodes them into characters. The character set it uses can be specified by name or explicitly given, or the platform default character set can be accepted.
Construction Method Summary
InputStreamReader (InputStream in)
creates a inputstreamreader that uses the default character set.
InputStreamReader (InputStream in, Charset CS)
creates inputstreamreader with the given character set.
InputStreamReader (InputStream in, Charsetdecoder Dec)
creates a inputstreamreader that uses the given character set decoder.
InputStreamReader (InputStream in, String charsetname)
creates inputstreamreader with the specified character set.
Each call to a read () method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable a valid conversion from byte to character, you can read more bytes from the underlying stream ahead of time so that it exceeds the byte needed to satisfy the current read operation.
In order to achieve maximum efficiency, you may want to consider packaging InputStreamReader in BufferedReader. For example:
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
The most important feature of InputStreamReader is that it can refer to the coded format of the conversion, which is not possible for other classes, as can be seen from the construction method, which is useful when reading Chinese characters
FileReader
1) FileReader class Introduction:
InputStreamReader class, All methods (read (), etc.) are inherited from the parent class InputStreamReader;
2 The difference from the InputStreamReader class:
Construction Method Summary
FileReader (File file)
Creates a new filereader given the File from which the data is being read.
FileReader (FileDescriptor FD)
Creates a new filereader given the filedescriptor from which to read the data.
FileReader (String fileName)
Creates a new FileReader given the file name from which the data is being read
The main difference between this class and its parent class is the constructor, the InputStreamReader is the constructor!
As you can see from the InputStreamReader constructor, the arguments are inputstream and encoded, and you see that you must use the InputStreamReader class when you want to specify the encoding method While the parameters of the FileReader constructor are the same as the FileInputStream, either as a file object or as a string representing path, you can see that when you want to read a file based on the file object or string, use FileReader;
I think the role of FileReader is also in this small division of Labor bar. The main difference between this class and its parent class is the constructor, the InputStreamReader is the constructor!
From InputStreamReader
constructor, you can see that the parameters are inputstream and encoded, and you must use the InputStreamReader class when you want to specify the encoding method, and the FileReader constructor parameters are the same as the FileInputStream. As a file object or a string that represents path, you can see that you use FileReader when you want to read one of the files based on the file object or string;
I think the role of FileReader is also in this small division of Labor bar.
Two relations and differences
(1) characters and bytes:
FileInputStream class with binary input/output, I/O is fast and efficient, but its read () method reads a byte (binary data), is not good for people to read, and can not directly manipulate the characters in the file, such as substitution, lookup (must be in byte form) ;
And the reader class makes up for this flaw, can be in text format input/output, very convenient, such as can use while ((ch = filereader.read ())!=-1) loop to read files; You can use BufferedReader readline () Method reads the text one line at a line.
(2) coding
InputStreamReader, it is the byte converted to the character of the bridge. You can specify the encoding in the constructor, and the default encoding of the underlying operating system, such as GBK, will be used if unspecified.
FileReader and InputStreamReader involve encoding conversion (specify encoding or adopt OS default encoding), may appear garbled phenomenon on different platform! and FileInputStream in binary way to deal with, will not appear garbled phenomenon.
Therefore, to specify the encoding method, you must use the InputStreamReader class, so that it is the byte converted to the character of the bridge;
(3) Buffer
The Bufferreader class is used to wrap all Reader (such as FileReader and InputStreamReader) whose read () operations can be expensive.
(4) Standard usage
Summarize the above and get a better standard usage:
1) file File = new file ("Hello.txt");
2) file File = new file ("Hello.txt");
FileInputStream in=new fileinputstream (file);
InputStreamReader inreader=new InputStreamReader (in, "UTF-8");
3) File File = new file ("Hello.txt");
FileReader filereader=new filereader (file);
BufferedReader bufreader=new BufferedReader (FileReader);