have been confused FileReader FileInputStream InputStreamReader bufferedreader The difference between the connection, each write read the document Java program is online Baidu. This issue was discussed in depth today.
First, the source
Java.io has two abstract class InputStream and Reader, the above classes are inherited by these two super classes. The difference between them is that InputStream is the input of a byte stream, and reader is a character stream input.
Second, each type of specific
InputStream
| __fileinputstream
The FileInputStream is integrated from the InputStream and can read files that develop file or path.
Reader
|--bufferedreader
|___inputstreamreader
|__filereader
BufferedReader: Reads text from the character input stream and buffers individual characters. The most important thing about it is that there is buffering, not reading from the underlying byte or character stream every time the read is like any other reader.
inputstreamreader: A bridge that streams bytes to a character stream. The important feature is that you can use the specified encoding format.
filereader: inputstreamreader, which differs from the parent class primarily in the constructor, is used when reading files based on file or string filereader.
Three, the main points
1.InputStreamReader, which is a bridge in which bytes are converted to characters. You can specify the encoding in the constructor, and if you do not specify it, the default encoding for the underlying operating system, such as GBK, is used.
FileReader and InputStreamReader involve encoding conversion (specifying encoding or using OS default encoding), which may appear garbled on different platforms! The FileInputStream is processed in binary mode, and there is no garbled phenomenon.
Therefore, to specify the encoding method, the InputStreamReader class must be used, so it is a bridge in which bytes are converted to characters;
2, in order to achieve the highest efficiency, can be considered in the BufferedReader packaging InputStreamReader. For example:
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
3.Specification Usage
summarize the above, and draw a good 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);
Java filereader fileinputstream inputstreamreader bufferedreader effect and difference