Streams in Java:
In fie processing, input refers to the flow of data into a program and output means the flow of data out of a program. Input to a program may come from various input devices like keyboard, mouse, memory, disk or any other program. The output from a program may go to screen, printer, memory, disk, network to other systems.
These devices look very different at the hardware level but they share certain common characteristics such as:
- Unidirectional flow of data
- Treating data as a sequence of bytes or characters.
- Support to the sequential access of data.
IO devices work as below:
Java uses the concept of Streams to represent the ordered sequence of data, a common characteristic shared by all input/output devices. A Stream presents a uniform, easy to use, the object-oriented interface between the program and input/output devices.
A Stream in java is a path along which data flows(like a pipeline of water supply). It has a source of data and a destination of the same data. The source and destination may be physical devices or code or other streams in the program.
The data flow via Streams:
Basic Classification of Streams:
Java streams are classified into two basic types:
- InputStreams-reads the data from a source and send it to the program for processing
- OutputStreams-write processed data to a Stream and send it over other devices.
The program connects and opens an InputStream on the data source and then read the data serially or sequentially. Similarly, the program connects and opens an OutputStream to the destination place of data and writes data on our serially or sequentially.
The program may not know the source or destination of the data at this point.
Ways to read from Stream in Java:
Java supports reading in the following ways:
- Random access
- Line y line
- by words
- by tokens
Further Classification of Stream classes:
Stream classes may be further divided into
- ByteStream- These classes help to handle I/O operation on bytes
- CharacterStream- These classes help to handle I/O operations on Characters.
|Java Stream class||Classification||Classes||Destination|
|CharacterStream Classes||Reader Classes||Reader Classes
|CharacterStream Classes||Writer Classes||BufferedWriter
ByteStream classes have been designed to provide a functional features for creating and manipulating Streams and files for reading and writing bytes. As the Streams are unidirectional, they can transmit bytes in only one direction and therefore java provides two kinds of byte stream classes:
Classification of ByteStream:
InputStream classes that are used to read 8-bit bytes include a superclass known as InputStream and several subclasses for supporting different input related functionalities. By inheritance, all classes derived from InputStream have basic methods called read() for reading a single byte or array of bytes.
The Superclass InputStream is an abstract class, we can not create an instance of this class. We need to use subclasses that inherit from this class. The inputStream class defines methods for performing the below operations:
- Reading bytes
- Closing Streams
- Marking position in Stream
- Shipping ahead in a Stream
- Finding the number of bytes in Stream
Class DataInputStream extends to FilterInputStream and implements the interface DataInput. Hence the DataInputStream class implements the methods described in the DataInput apart from its methods.
OutputStream classes are derived from the abstract base class OutputStream. All classes derived from OutputStream have basic method write() for writing a single byte or array of bytes. We can not instantiate the OutputStream class. The OutputStream class does the below operations:
- Writing bytes
- Closing streams
- Flushing streams
The WriterStream classes are designed to perform all output related operations on files. While OutputStream classes are used to write bytes the WriterStream classes are used to write characters. The Writer class is an abstract class which acts as a base class for all the other WriterStream classes. The base class provides support for all output operations by defining the methods that are identical to the OutputStream class.
The class StreamTokenizer a subclass of Object can be used to break up a Stream of text from a file into meaningful pieces called tokens. It is Similar to StringTokenizer class that breaks a String into its component String Tokens.