Different Streams in Java

0
175
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

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:

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

IO devices work as below:

 

Source flow Mediator Destination
keyboard
Mouse
Memory
Disk
Network
Another program
————–> Java code ————–> Screen
Printer
Memory
Disk
Network
Another program

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:

 

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

  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 help to handle I/O operation on bytes
  2. CharacterStream- These classes help to handle I/O operations on Characters.

 

Java Stream class Classification Classes Destination
ByteStream Classes InputStream FileInputStream
PipedInputStream
ByteArrayInputStream
SequentialInputStream
ObjectInputStream
StringBufferInputStream
FileterInputSream
|__BufferedInputStream
|__ PushbackInputStream
|__ LineNumberInputStream
|__ DataInputStream
|–> DataInput
Memory
File
Pipe
ByteStream Classes OutputStream FileOutputStream
PipedOutputStream
ByteArrayOutputStream
SequentialOutputStream
ObjectOutputStream
FileterOutputSream
|__BufferedOutputStream
|__ PushbackOutputStream
|__ DataOutputStream
|–>DataOutput
Memory
File
Pipe
CharacterStream Classes Reader Classes Reader Classes
|__LineNumberReader
|__InputstreamReader 
Memory
File
Pipe
CharacterStream Classes Writer Classes BufferedWriter
CharArrayWriter
FilterWriter
PrintWriter
StringWriter
PipeWriter
OutputStreamWriter
|__FileWriter
Memory
File
Pipe

ByteStream Classes:

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:

  1. InputStream
  2. OutputStream

InputStream Classes:

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:

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

OutputStream Classes:

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:

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

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here