Java IO reader and writer
2011-08-24 16:05
405 查看
Java IO's
Here is a simple example:
Notice, that while an
A
In the constructor you can also specify what character set to use to decode the text etc. More on that in the text on
Here is a simple example:
A
Readerand
Writerwork much like the
InputStreamand
OutputStreamwith the exception that
Readerand
Writerare character based. They are intended for reading and writing text. The
InputStreamand
OutputStreamwere byte based, remember?
Reader
TheReaderis the baseclass of all
Reader's in the Java IO API. Subclasses include a
BufferedReader,
PushbackReaderetc.
Here is a simple example:
Reader reader = new FileReader(); int data = reader.read(); while(data != -1){ char dataChar = (char) data; data = reader.read(); }
Notice, that while an
InputStreamreturns one byte at a time, meaning a value between -128 and 127, the
Readerreturns a
charat a time, meaning a value between 0 and 65535. This does not necessarily mean that the
Readerreads two bytes at a time from the source it is connected to. It may read one or more bytes at a time, depending on the encoding of the text being read.
A
Readercan be combined with an
InputStream. If you have an
InputStreamand want to read characters from it, you can wrap it in an
InputStreamReader. Pass the
InputStreamto the constructor of the
InputStreamReaderlike this:
Reader reader = new InputStreamReader(inputStream);
In the constructor you can also specify what character set to use to decode the text etc. More on that in the text on
InputStreamReader.
Writer
TheWriterclass is the baseclass of all
Writer's in the Java IO API. Subclasses include
BufferedWriterand
PrintWriteramong others.
Here is a simple example:
Writer writer = new FileWriter("c:\\data\\file-output.txt"); writer.write("Hello World Writer"); writer.close();
A
Writercan be combined with an
OutputStreamjust like
Readersand
InputStream's. Wrap the
OutputStreamin an
OutputStreamWriterand all characters written to the
Writerare passed on to the
OutputStream. Here is how that looks:
Writer writer = new OutputStreamWriter(outputStream);
Combining Readers and Writers
Just like with streams,Reader's and
Writer's can be combined into chains to achieve more interesting IO. It works just like combining the
Readerwith
InputStream's or the
Writerwith
OutputStream's. For instance, you can achieve buffering by wrapping a
Readerin a
BufferedReader, or a
Writerin a
BufferedWriter. Here are two such examples:
Reader reader = new BufferedReader(new FileReader(...)); Writer writer = new BufferedWriter(new FileWriter(...)
相关文章推荐
- Java_IO_字节流_Reader_and_Writer
- JavaIO(04)字符流--Writer and Reader
- [Java] IO-01 FileInputStream / FileOutputStream / FileReader / FileWriter
- Java之IO(十二)CharArrayReader和CharArrayWriter
- 黑马程序员 java基础IO FileReader FileWriter
- Java IO操作——字节-字符转换流(OutputStreamWriter、InputStreamReader)
- java IO笔记(CharArrayReader/CharArrayWriter)
- [Java] IO-01 FileInputStream / FileOutputStream / FileReader / FileWriter
- Java - IO - 字符流 - Writer - Reader
- java BufferedWriter and BufferedReader
- Java IO 文件流的读,文件流写Writer,Reader,InputStream,OutputStream封装
- java IO,bufferedReader/bufferedWriter对文件的读写
- Java_io体系之CharArrayReader、CharArrayWriter简介、走进源码及示例——13
- Java-IO之字符输入输出流(Reader和Writer)
- Java IO深入理解字节和字符数组(ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter)
- java FileWriter and FileReader
- 学习笔记之java.io包中的字符流(上)—— Reader和Writer
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java-IO之字符输入输出流(Reader和Writer)
- think in java - io - reader/writer