Java Io:reader and Writer

Source: Internet
Author: User
Tags ranges

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

In addition to character-based, reader and writer for Java Io are very similar to InputStream and OutputStream. They are used to read and write text. InputStream and OutputStream are byte-based, remember?

Reader

The reader class is the base class for all reader in Java IO. Subclasses include Bufferedreader,pushbackreader,inputstreamreader,stringreader and other reader.

This is an example of a simple Java IO reader:

01 Reader reader = newFileReader("c:\\data\\myfile.txt");
02
03 intdata = reader.read();
04
05 while(data != -1){
06
07     chardataChar = (char) data;
08
09     data = reader.read();
10
11 }

Note that the read () method of InputStream returns a byte, meaning that the return value ranges from 0 to 255 (when the end of the stream is reached, 1 is returned), and the Read () method of reader returns a character, meaning that the return value ranges from 0 to 65535 ( When the end of the stream is reached, the same returns-1). This does not mean that Reade only reads 2 bytes at a time from the data source, and reader reads one or more bytes at a time based on the encoding of the text.

You will typically use the subclass of reader instead of using reader directly. The subclass of reader includes Inputstreamreader,chararrayreader,filereader and so on. You can view the Java IO Overview to browse through the complete reader form.

integrating reader with InputStream

A reader can be combined with a inputstream. If you have a InputStream input stream and want to read the characters from it, you can wrap the InputStream into InputStreamReader. Pass the InputStream to the InputStreamReader constructor:

1 Reader reader = newInputStreamReader(inputStream);

You can specify the decoding method in the constructor. For more information, see InputStreamReader.

Writer

The writer class is the base class for all writer in Java IO. Subclasses include BufferedWriter and PrintWriter, and so on. This is an example of a Java IO writer:

1 Writer writer = newFileWriter("c:\\data\\file-output.txt"); 
2
3 writer.write("Hello World Writer"); 
4
5 writer.close();

Again, you'd better use writer's subclass, without having to use writer directly, because the subclasses are more explicit in their implementation and more expressive of your intentions. Common subclasses include Outputstreamwriter,chararraywriter,filewriter and so on. Writer's write (int c) method writes the low 16 bits of the passed-in parameter to writer, ignoring the high 16-bit data.

integrating writer and OutputStream

Like reader and InputStream, a writer can be combined with a outputstream. Wrap the OutputStream into the OutputStreamWriter, and all characters written to OutputStreamWriter will be passed to OutputStream. This is an example of a outputstreamwriter:

Writer writer = new OutputStreamWriter (outputstream);

integrating reader and writer

Like the byte stream, reader and writer can combine to achieve more and more interesting Io, working in a similar way to combining reader with InputStream or writer and outputstream. For a chestnut, you can buffer the reader by wrapping it into BufferedReader, writer wrapper, and BufferedWriter. Here is an example:

1 Reader reader = newBufferedReader(newFileReader(...));
2
3 Writer writer = newBufferedWriter(newFileWriter(...));

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

Java Io:reader and Writer

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.