Contents
- 1 Streams in Java:
- 1.1 IO devices work as below:
- 1.2 The data flow via Streams:
- 1.3 Basic Classification of Streams:
- 1.4 Ways to read from Stream in Java:
- 1.5 Further Classification of Stream classes:
- 1.6 ByteStream Classes:
- 1.7 Classification of ByteStream:
- 1.8 InputStream Classes:
- 1.9 OutputStream Classes:
- 1.10 Share and Enjoy !
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:
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:
- 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:
- Sequential
- Random access
- binary
- Characters
- 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 |
---|---|---|---|
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 |__BufferedReader | Memory File Pipe |
CharacterStream Classes | Writer Classes | BufferedWriter CharArrayWriter FilterWriter PrintWriter StringWriter PipeWriter OutputStreamWriter |__FileWriter | Memory File Pipe |
Apart from these, we use the below streams for serialization:
- ObjectStreamClass
ByteStream Classes:
ByteStream classes have been designed to provide 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
- 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:
- 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:
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.