Tuesday, May 10, 2011

Using the Data Sink Streams

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 gives java.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 and CharArrayWriter
ByteArrayInputStream and ByteArrayOutputStream
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 and FileWriter
FileInputStream and FileOutputStream
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 and FileWriter to copy the contents of one file into another.
PipedReader and PipedWriter
PipedInputStream and PipedOutputStream
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 and PipedWriter in action in How to Use Pipe Streams.
StringReader and StringWriter
StringBufferInputStream
Use StringReader to read characters from a String as it lives in memory. Use StringWriter to write to a String. StringWriter collects the characters written to it in a StringBuffer, which can then be converted to a String. StringBufferInputStream is similar to StringReader, except that it reads bytes from a StringBuffer.

No comments:

Post a Comment

Chitika