FileReader FileInputStream inputstreamreader BufferedReader function and difference of Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.