Java Io:inputstreamreader and OutputStreamWriter

Source: Internet
Author: User

Author: Jakob Jenkov Translator: Li Jing ([email protected])

This section provides a brief introduction to InputStreamReader and OutputStreamWriter. The attentive reader may find that in the previous article, the classes in IO either end with a stream or end in reader or writer, what is the purpose of these two classes that end with the class name suffix of the byte stream and the character stream? Simply put, these two classes convert bytes into character streams, with data conversion in between, similar to the idea of adapter mode.

InputStreamReader

Original link

The InputStreamReader will contain a inputstream, which will allow the input byte stream to be converted into character streams, code examples:

01 InputStream inputStream = newFileInputStream("c:\\data\\input.txt");
02
03 Reader reader = newInputStreamReader(inputStream);
04
05 intdata = reader.read();
06
07 while(data != -1){
08
09     chartheChar = (char) data;
10
11     data = reader.read();
12
13 }
14
15 reader.close();

Note: For clarity, the code ignores some of the necessary exception handling. For more information about exception handling, refer to Java IO exception handling.

The Read () method returns a variable of type int that contains the contents of the read character (translator note: 0~65535). The code is as follows:

1 intdata = reader.read();

You can convert the returned int value to a char variable, like this:

1 charaChar = (char) data; //译者注:这里不会造成数据丢失,因为返回的int类型变量data只有低16位有数据,高16位没有数据

If the method returns-1, indicating that there are no remaining readable characters in reader, you can turn reader off. 1 is an int type, not a byte or char type, which is not the same.

InputStreamReader also has other optional constructors that allow you to specify the stream of characters that the underlying stream of bytes will be interpreted as encoded. Examples are as follows:

1 InputStream inputStream = newFileInputStream("c:\\data\\input.txt");
2
3 Reader reader = newInputStreamReader(inputStream, "UTF-8");

Note the second parameter of the constructor, at which point the InputStreamReader converts the bytes of the input into a stream of UTF8 characters.

OutputStreamWriter

Original link

The OutputStreamWriter will contain a outputstream, which allows the output byte stream to be converted into a character flow with the following code:

1 OutputStream outputStream = newFileOutputStream("c:\\data\\output.txt");
2
3 Writer writer = newOutputStreamWriter(outputStream);
4
5 writer.write("Hello World");
6
7 writer.close();

The OutputStreamWriter also has a constructor that converts the output byte flow into a character stream of the specified encoding.

original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java Io:inputstreamreader and OutputStreamWriter

Java Io:inputstreamreader and OutputStreamWriter

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.