The relationship between InputStream, InputStreamReader and Reader

Source: Internet
Author: User

InputStream: Gets the byte input stream, Inputstream.read ("filename"),

Reader: Reading a stream of characters

InputStreamReader: A bridge from bytes to characters. InputStreamReader (Inputstream.read ("filename"));

Reader.read (InputStreamReader (InputStream in)), which can be changed from byte to character, printed and displayed.

Java.io.Reader and Java.io.InputStream make up the Java input class.

Reader is used to read 16-bit characters, which are Unicode-encoded characters, while InputStream is used to read ASCII characters and binary data.

Reader supports 16-bit Unicode character output,

The InputStream supports 8-bit character output.

Reader and InputStream are two sets of parallel independent hierarchies provided by the I/O library,

1byte = 8bits

InputStream, OutputStream is used to process a 8-bit stream,

Reader, writer, is used to handle 16-bit streams.

In the Java language, the byte type is 8 bits, and the char type is 16 bits, so you need to use reader and writer when working with Chinese.

It is worth noting that, under these two hierarchies, there is also a bridge inputstreamreader, OutputStreamWriter is responsible for InputStream to reader adaptation and from OutputStream to writer adaptation.

In Java, there are different types of Reader input streams that correspond to different data sources:

FileReader is used to input from a file; CharArrayReader is used to enter from a character array in a program, StringReader is used to enter from a string in a program, Pipedreader is used to read pipedwriter from another thread Writes the data to the pipeline.

There are also different types of inputstream input streams corresponding to different data sources: Fileinputstream,bytearrayinputstream,stringbufferinputstream, PipedInputStream.

In addition, there are two inputstream input streams that do not correspond to the Reader type: socket for sockets; URLConnection for URL connections. These two classes use getInputStream () to read the data.

Accordingly, Java.io.Writer and Java.io.OutputStream also have similar differences.

With regard to Inputstream.read (byte[] b) and Inputstream.read (byte[] b,int Off,int len) Both methods are used to read multiple bytes from the stream, and experienced programmers will find that these two methods are often You cannot read the number of bytes that you want to read. For example, the first method, programmers often want the program to read B.length bytes, and the reality is that the system is often not read so much. A closer look at the Java API description reveals that this method does not guarantee that you can read so many bytes, it can only guarantee to read so many bytes (at least 1). Therefore, if you want a program to read count bytes, it is best to use the following code:
Byte[] B = new Byte[count];
int readcount = 0; The number of bytes that have been successfully read
while (Readcount < count) {
Readcount + = in.read (bytes, readcount, count-readcount);
}
Use this code to ensure that count bytes are read unless an IO exception is encountered halfway through or at the end of the data stream (eofexception)

The relationship between InputStream, InputStreamReader and Reader

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.