Skip to content

Chapter 6 : Streams and File IO

Saket Khopkar edited this page Jan 7, 2022 · 1 revision

Introduction:

  • So far we have used variables and arrays for storing data. But this approach has two problems:
  1. The storage is temporary.
  2. It is difficult to handle large amount of data.
  • Above problems can be solved by storing data on secondary storage using the concept of file. Data stored in the file is called persistent data.
  • A file is a collection of related records stored on the disk.
  • A record is composed of several fields and a field is a group of characters. Characters in Java are Unicode characters made up of 2 bytes each.
  • Storing and managing data using file is known as file processing. It includes:
  1. Crating files
  2. Updating files
  3. Manipulation of data
  • Java provides powerful features of input and output of data using file.
  • Reading and writing data can be done at:
  1. Byte level
  2. Character level
  • Java provides capabilities to read and write class object directly.
  • The process of reading and writing object is called object serialization.

Concept of Streams:

  • A stream is a continuous group of data or a path along which data flows from one point to another.
  • Java streams are classified into two basic types:
  1. Input Stream takes input data from source and moves it into a program.
  2. Output Stream takes output data generated from program and moves it to a destination.
  • A stream presents a uniform, easy to use, object-oriented interface between the program and input / output devices.
  • Stream is a powerful file processing tool in Java, which can be used to get data in one format and convert it into another format. For example, one stream can be used to get raw data in binary format and another stream can be used to convert it to integers.
  • In file processing, input refers to data flow into a program and output refers to data flow out of a program.
  • Standard Input Output Stream in Java represented by three members of System classes:
  1. System.in : Input Stream
  2. System.out : Output Stream
  3. System.err : Error Stream

Stream Classes:

  • The java.io package contains a large number of stream classes that provide capabilities for processing all type of data.
  • These classes are of two types:
  1. Byte stream: Support for I/O operations on byte.
  2. Character Stream: Support I/O operations on characters.
  • These classes are further classified according to their functions.

Byte stream classes

  • provide functional features for creating and manipulating streams and files for reading and writing bytes.
  • Byte stream classes handle the data in 8 bit format.
  • It contains InputStream and OutputStream as its fundamental classes.
  • Streams are unidirectional; hence they can transmit bytes in only one direction.

Input Stream Classes:

  • It reads data in 8 bit format.
  • Here InputStream is a super class and number of subclasses for supporting various input related functions.
  • InputStream is an abstract class, so we cannot create its instance.
  • InputStream class define methods for performing input functions such as:
  1. Reading bytes
  2. Closing Streams
  3. Marking positions in streams
  4. Skipping ahead in a stream
  5. Finding the number of bytes in a stream