Quick charts of I/O classes

 

 

READER INPUT CLASSES

 

Arrows correspond to the stream direction which is implicit in combined constructor statements. 

 i.e. right to left for input,      and left to right for output.

 

The arrows below indicate where other chained streams are likely to be attached.  Check out the combined constructors.

 

chained sink stream   BufferedInputStream   chained source stream

DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream( “myfile”)));

 

 

It sends to
what
sink?

CHARACTER STREAM
INPUT
CLASS

It gets from
what
source?

Primary Function
of the Class:

Does it provide any unique methods beyond those in Reader?

 

CHAINABLE SINK:
If chained to a sink stream, it provides a Reader stream of buffered characters to that stream. 

 

If not chained, its methods can be used return the characters. You may also want to use its readLine( ) method.

 

BufferedReader

CHAINABLE SOURCE: Its source can be any Reader stream.

Provides buffered reading.

 

It is usually chained to a FileReader as its source stream.

It adds the handy  readLine( ) method.

readLine( ) reads until encountering a  \n, \r, or \r\n and then stops, returning the whole line in a String.

 

CHAINABLE SINK:
 If chained to a sink stream, it provides a Reader stream of characters from the array.  

 

If not chained, its methods can be used to return the chars from the source array.

 

   CharArrayReader

SOURCE NOT CHAINABLE:
Its source is always  an array of chars specified in its constructor

Creates a Reader stream by reading from a char array

Use the 9 methods from Reader.

 

Adds nothing. 

 

CHAINABLE SINK:
If chained to a sink stream, it provides a Reader stream of the characters
from the file.  

 

If not chained, its read(..) methods can be used to simply return the characters. 

 

  FileReader

SOURCE NOT CHAINABLE: 
Its source is always a file from the file system.

Reads a file.

Use the 9 methods from Reader.

 

Adds nothing.

 

CHAINABLE SINK:
If chained to a sink  stream, it provides a Reader stream of characters which were bytes when they came in. 

 

If not chained,  its methods can be used to return values.  You may simply want to use its read(..) methods.

 

InputStreamReader  

CHAINABLE SOURCE: Its source can be an InputStream stream consisting of bytes or of various other Charsets, including Unicode.

 

It does not take a Reader stream.

Converts InputStream source bytes into a Reader stream of characters.

Use the 9 methods from Reader.


It adds one getEncod-ing( ) method.

 

USUALLY NOT CHAINED:  Because you will probably want to use its own getLineNumber(..) method.

 

LineNumberReader  

 

CHAINABLE SOURCE: Its source can be any Reader stream

 

Keeps track of line numbers in a Reader stream when reading lines with readLine( ).

Use the 9 methods from Reader and especially use  readLine( ) from its immediate parent BufferedReader.


It adds two methods -  getLineNumber( ) and setLineNumber(..).

 

CHAINABLE SINK:
If chained to a sink stream, it provides a Reader stream of characters which emanated from the connected PipedWriter stream.
  

 

If not chained, its methods return the characters from the source PipedWriter stream.

 

PipedReader   

SOURCE NOT CHAINABLE:
Its source is always the  PipedWriter stream to which it is connected.

Reads from a PipedWriter stream which has been previously written to.

Use the 9 methods from Reader. 


It adds one connect(.. )  method.

 

USUALLY NOT CHAINED: Because you will probably want to use  its unread(..) pushback methods.

 

PushBackReader  

CHAINABLE SOURCE: Its source can be any Reader stream

Reads from a Reader stream with the ability to put one or more chars back as if they had not been read.

Use the 9 methods from Reader. 


It adds three unread(..) methods for pushing characters back into the stream. 

 

CHAINABLE SINK:

If chained to a sink stream, it provides a Reader stream of characters from the String named in its constructor.

 

If not chained, its methods return values from the source String.

 

  StringReader

SOURCE NOT CHAINABLE:

Its source is a String specified in its constructor

Lets you apply Reader I/O stream methods to a String.

Use the 9 methods from Reader.

 

Adds nothing.

 

 

 

 

WRITER OUTPUT CLASSES

 

It gets from
what
source?

CHARACTER STREAM
OUTPUT
 
CLASS

It sends to
what
sink?

Primary Function
of the class:

Does it provide any unique methods beyond those in Writer?

 

CHAINABLE SOURCE: Its source can be any Writer stream. If not chained, its methods must be supplied with values to write.

 

BufferedWriter

 

CHAINABLE SINK:

Its Writer output stream can go into any other output Writer stream.

 

Provides buffered  writing.  It is usually chained to a FileWriter stream which acts as a sink.

Use the 7 methods from Writer. 

 

It adds a newLine( ) method which writes a line separator.

 

CHAINABLE SOURCE: Its source can be any Writer output stream.  If not chained, its methods must be supplied values to write.

 

CharArrayWriter

 

SINK NOT CHAINABLE: The CharArrayWriter is a sink itself.  Its contents are converted to a char array or String, or they can be written to another stream using writeTo( ), or, after creation,  the now-filled stream object handle can be passed to a subsequent stream declaration constructor.

 

Allows I/O methods to be used to write an internal buffer which can later be retrieved as a char array or as a String.

Use the 7 methods from Writer. 

 

It adds five methods: size( ), writeTo( ), and reset( ), plus toCharArray( ) and toString( ) for converting the stream contents.

 

CHAINABLE SOURCE: Its source can be any Writer output stream. If not chained, its methods must be supplied values to write.

 

FileWriter

SINK NOT CHAINABLE: Its sink is always a file in the file system.

Writes to a file in the file system.

Use the 7 methods from Writer.

 

Adds nothing.

 

 

CHAINABLE SOURCE: Its source can be any Writer output stream.  If not chained, its methods must be supplied values to write.

 

FilterWriter

CHAINABLE SINK:

Its Writer output stream can go into any other output Writer stream sink.

Provides a convenience class to override if you want to write your own filtering.

Adds nothing to Writer's 7 methods.

 

CHAINABLE SOURCE: Its source can be any Writer output stream. If not chained, its methods must be supplied values to write.

 

OutputStreamWriter

 

CHAINABLE SINK: However its sink is always an OutputStream,

for instance a FileOutputStream.

 

Can produce bytes as well as characters.

Use the 7 methods from Writer. 


It adds one getEncoding( ) method.

 

CHAINABLE SOURCE: Its source can be characters written to any Writer output stream.  If not chained, its methods must be supplied values to write.

PipedWriter

SINK NOT CHAINABLE: The PipedWriter is a sink itself.  Its contents are transferred automatically into the PipedReader to which it is connected.

Creates a Writer stream which is a source input to a PipedReader.

 

USUALLY NOT CHAINED: Because you will want to use its  print(..) or println(..)  methods.

 

PrintWriter

 

CHAINABLE SINK:

Its sink is always a  character output stream such as a FileWriter. Or it can also go to a byte OutputStream such as a FileOutputStream.

 

Writes or displays Java primitives and objects as character text.

It adds 21 print( ..) and println(..) methods to  the 7 methods in Writer.

 

CHAINABLE SOURCE: Its source can be characters from any Writer output stream.  If not chained, its methods must be supplied values.

 

StringWriter

 

SINK NOT CHAINABLE: The StringWriter is a sink itself.  Its contents are  converted to a String or StringBuffer or its object handle can be passed to a subsequent stream declaration constructor.

 

Allows I/O methods to be used on Strings.

Use the 7 methods from Writer. 

 

It adds two methods - getBuffer( ) plus toString( ) for converting the stream contents.