Streams in Java

Stream Concept in Java
Streams in Java

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. 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:

  1. Unidirectional flow of data
  2. Treating of data as a sequence of bytes or characters.
  3. Support to the sequential access of data.

IO devices works as below:

Source flow Mediator Destination
Another program


Java code

Another program
Java uses the concept of Streams to represent the ordered sequence of data,a common characteristics shared by all input/output devices.A Stream presents an uniform ,easy to use ,object oriented interface between the program and input/output devices.
A Stream in java is a path along which data flows(like pipeline of a 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:

Source flow code Program code Destination
Source data -------------->
input Stream
Java code
read operation
Process data
Java code
write operation
Output Stream

Basic Classification of Streams:

Java streams are classified into two basic types:

  1. InputStreams-reads the data from a source and send it to program for processing
  2. 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 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:
  1. Sequential
  2. Random access
  3. binary
  4. Characters
  5. Line y line
  6. by words 
  7. by tokens

Further Classification of Stream classes:

Stream classes may be further divided into 
  1. ByteStream- These classes helps to handle I/O operation on bytes
  2. CharacterStream- These classes helps to handle I/O operations on Characters.

Java Stream class Classification Classes Destination
ByteStream Classes InputStream FileInputStream
|__ PushbackInputStream
|__ LineNumberInputStream
|__ DataInputStream
|--> DataInput
ByteStream Classes OutputStream FileOutputStream
|__ PushbackOutputStream
|__ DataOutputStream
CharacterStream Classes Reader Classes Reader Classes Memory
CharacterStream Classes Writer Classes BufferedWriter

ByteStream Classes:

ByteStream classes have been designed to provide functional feature 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:

  1. InputStream
  2. OutputStream

InputStream Classes:

InputStream classes that are used to read 8-bit bytes includes a super class known as InputStream and a number of sub classes 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 Super class InputStream is an abstract class,we can not create instance of this class.We need to use subclasses that inherit from this class. The inputStream class defines methods for performing the below operations:
  1. Reading bytes
  2. Closing Streams
  3. Marking position in Stream
  4. Shipping ahead in a Stream
  5. 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 own methods.

OutputStream Classes:

OutputStream classes are derived from the abstract base class OutputStream. All classes derived from OutputStream have basic method write() for writting a single byte or array of bytes.We can not instantiate the OutputStream class. The OutputStream class does the below operations:
  1. Writing bytes
  2. Closing streams
  3. 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 OutputStream class.

The class StreamTokenizer a subclass of Object can be used to break up a Stream of text from file into meaningful pieces called tokens.It is Similar to StringTokenizer class that breaks a String into its component String Tokens.
Streams in Java Streams in Java Reviewed by Animesh Chatterjee on January 09, 2019 Rating: 5

No comments:

Powered by Blogger.