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 = new FileReader( "c:\\data\\myfile.txt" ); |
03 |
int data = reader.read(); |
07 |
char dataChar = ( char ) data; |
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 = new InputStreamReader(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 = new FileWriter( "c:\\data\\file-output.txt" ); |
3 |
writer.write( "Hello World Writer" ); |
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 = new BufferedReader( new FileReader(...)); |
3 |
Writer writer = new BufferedWriter( new FileWriter(...)); |
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