One of the important set of classes comprised in Java API is the java.io package. This is one of the core packages of the Java language and was a part of JDK 1.0. These classes facilitate the input/output functionality in our programs.

Typical examples of such functionalities include:

  •   Reading from the keyboard
  •   Sending some output to the console
  •   Storing data in a disk file
  •   Chatting with another user using peer-to-peer networking
  •   Transferring files
  •   Browsing the Web, and so on.

 Thus, the I/O classes are used in a wide range of applications, including the latest innovations such as voice and video calls, peer-to-peer gaming, and more.

Input/Output Streams

We may not have realized this, but we have already used some functionality of I/O classes in the previous examples. In many of our programs, we used System.out.println to output a message to the user console.

We learned earlier that:

  •   println is a method executed on the out object.
  •   The out object is of type OutputStream, which is an abstract class. It is the superclass of all classes representing an output stream of bytes.
  •   A stream accepts bytes and sends them to some sink

Likewise, to read input from the user, we used the System.in.read method.

  •    The in is an object of type InputStream.
  •    Both InputStream and OutputStream belong to the family of I/O classes.
  •   The System class, which is defined in the java.lang package, contains three static fields, called in, out, and err.
  •  The in is of type InputStream, whereas out and err are of the PrintStream type, which is a subclass of OutputStream.

Java defines the functionality of its various I/O classes through streams. A stream is an abstraction and can be thought of as a flow of data from a source to a sink.

A stream can be classified in two ways.
  •    A source stream, also called an input stream, initiates the flow of data.
  •    A sink stream, also called an output stream, terminates the flow of data.

Source and sink streams are also called node streams. A stream is just a continuous flow of data. Like an array that holds some data, a stream does not have the concept of a data index. We cannot move back and forth in a stream. The data can only be accessed sequentially.

A stream either consumes or provides information. A stream is usually linked to a physical device. It provides a uniform interface to a device for data flow. In the case of an input stream, the device to which it connects may be a physical disk, a network connection, a keyboard, and so on.

In the case of an output stream, it may be connected to a console, a physical disk, a network connection, and so on. Thus, when we use the input/output stream classes, our program code becomes independent of the device to which the stream connects. Examples of source streams are files and memory buffers. A printer or a console can represent a stream destination.

The streams in Java are of two types:
  •    Byte oriented
  •    Character oriented

The byte streams operate on bytes of data, whereas the character-oriented streams operate on characters, typically a Unicode character set. JDK 1.0 provided only byte-oriented streams. JDK 1.1 introduced character-oriented streams. Because the underlying mechanism for streams is still byte oriented, JDK 1.1 also introduced bridge classes to convert a byte stream into a character stream, and vice versa.

  Modified On Mar-15-2018 04:10:38 AM

Leave Comment