Data sink streams read from or write to specialized data sinks such as strings, files, or pipes. Typically, for each reader or input stream intended to read from a specific kind of input source, java.io contains a parallel writer or output stream that can create it. The following table givesjava.io
's data sink streams.
Sink Type Character Streams Byte Streams Memory CharArrayReader
,CharArrayWriter
ByteArrayInputStream
,ByteArrayOutputStream
StringReader
,StringWriter
StringBufferInputStream
Pipe PipedReader
,PipedWriter
PipedInputStream
,PipedOutputStream
File FileReader
,FileWriter
FileInputStream
,FileOutputStream
Note that both the character stream group and the byte stream group contain parallel pairs of classes that operate on the same data sinks. These are described next:
CharArrayReader
andCharArrayWriter
ByteArrayInputStream
andByteArrayOutputStream
- Use these streams to read from and write to memory. You create these streams on an existing array and then use the read and write methods to read from or write to the array.
FileReader
andFileWriter
FileInputStream
andFileOutputStream
- Collectively called file streams, these streams are used to read from or write to a file on the native file system. How to Use File Streams has an example that uses
FileReader
andFileWriter
to copy the contents of one file into another.PipedReader
andPipedWriter
PipedInputStream
andPipedOutputStream
- Implement the input and output components of a pipe. Pipes are used to channel the output from one program (or thread) into the input of another. See
PipedReader
andPipedWriter
in action in How to Use Pipe Streams.StringReader
andStringWriter
StringBufferInputStream
- Use
StringReader
to read characters from aString
as it lives in memory. UseStringWriter
to write to aString
.StringWriter
collects the characters written to it in aStringBuffer
, which can then be converted to aString
.StringBufferInputStream
is similar toStringReader
, except that it reads bytes from aStringBuffer
.
A java blog with a collection of examples and tutorials on Java and related technologies. (Under maintenance with continuous updates) Be in touch with java jazzle or k2java.blogspot.com.
Tuesday, May 10, 2011
Using the Data Sink Streams
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment