J2EEOnlineJ2EE 





Java Streams  «Prev  Next»
Lesson 7

Java Input Streams Conclusion

In this module, you were introduced to reading data by using input streams. You learned:
  1. How to efficiently read data from a stream
  2. How to use the available() method to count available bytes in a stream
  3. How to skip unnecessary data
  4. When to close a stream
The java.io.InputStream class is the abstract superclass for all input streams. It declares the three basic methods needed to read bytes of data from a stream. It also has methods for closing and flushing streams, checking how many bytes of data are available to be read, skipping over input, marking a position in a stream and resetting back to that position, and determining whether marking and resetting are supported.
public abstract int read() throws IOException
public int read(byte[] data) throws IOException
public int read(byte[] data, int offset, int length) throws IOException
public long skip(long n) throws IOException
public int available() throws IOException
public void close() throws IOException
public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException
public boolean markSupported()

Input streams

Class java.io.InputStream is an abstract base class for all the input streams in Java. Itís extended by all the classes that need to read bytes (for example, image data) from multiple data sources. Letís start with the most important method of this class, read(), used to read data from a source. The class InputStream defines multiple overloaded versions of method read(), which can be used to read a single byte of data as int, or multiple bytes into a byte array:

int abstract read()
int read(byte[] b)
int read(byte[] b, int off, int len)


But you wouldnít use these methods yourself. Youíd use method read() by morespecific classes that extend the abstract class InputStream. For example, class File-InputStream extends InputStream and overrides its read() method for you to use.
Watch out for the use of method read() from class Input-Stream. It returns the next byte of data, or -1 if the end of the stream is reached. It doesnít throw an EOFException.
Method close() is another important method of class InputStream. Calling close() on a stream releases the system resources associated with it. Because InputStream is an abstract class, you canít create an instance to read from a data source or destination. You need one of its subclasses to do the work for you. So why do you need to bother with so much theory about it?

Why do you need to bother with so much theory on InputStream?

On the exam, youíll be tested on whether you can call a particular method on an object. For example, you might be tested on whether you can call the method read() or close() on an object of, say, FileInputStream, a subclass of InputStream. Because methods read() and close() are defined in class InputStream, they can be called on objects of any derived classes of InputStream. These might not be straightforward questions. You need to get the hang of the basics to answer them correctly.

Data Within Streams

  1. Java technology supports two types of streams: character and byte.
  2. Input and output of character data is handled by readers and writers.
  3. Input and output of byte data is handled by input streams and output streams:
a) Normally, the term stream refers to a byte stream. b) The terms reader and writer refer to character streams

Coming up

In the next module you will learn about writing data by using output streams.