Java Reference
In-Depth Information
The Input Stream Classes
For each output stream class, there is a corresponding input stream class for
reading in the data. The abstract InputStream class is the parent class of all the
input streams, and it contains read() methods that correspond to the write()
methods of the OutputStream class. It also contains several other methods,
including:
public int read(). Reads a single byte from the stream.
public int read(byte [] b). Reads in a collection of bytes and places them
in the given array. The return value is the actual number of bytes read.
public int read(byte [] b, int offset, int length).
Reads a collection of
bytes into the specified location of the array.
public void close(). Closes the stream.
public long skip(long n). Skips the next n bytes in the stream.
public void mark(int readLimit). Marks the current location of the
stream. This method is used in conjunction with the reset() method on
streams that allow support for pushback. After readLimit number of
bytes are read, the marked location becomes invalid.
public void reset(). Resets the position of the stream to the location of the
most recent mark() call, assuming that the marked location is still valid.
public int available(). Returns the number of bytes that can be read from
the input stream without blocking.
A stream (or reader/writer) is opened automatically when the object is
instantiated. You can close a stream by using the close() method. This is a
good programming design, but not mandatory. The garbage collector
implicitly closes a stream before the corresponding stream object is
garbage collected.
The Writer Class
The Writer class is the parent of all the writer classes in the java.io package. A
writer is used for outputting data that is represented as characters. The Writer
class contains the flush() and close() methods, which are similar to the ones in
OutputStream. It also contains five write() methods:
public void write(int c). Writes a single character to the stream.
public void write(char [] c).
Writes the array of characters to the stream.
Search WWH ::




Custom Search