Java Tutorial

IO Stream

Generally, in JAVA programming we write two types of applications or programs. They are volatile or non-persistent program and non-volatile or persistent program.

  1. A volatile program is one whose result is always stored in main memory of the computer i.e., RAM. Whatever the data which is stored in the main memory of the computer that data will be temporary i.e., the data which is available in main memory is volatile.
  2. A non-volatile program is one whose result is stored in secondary storage devices i.e., hard disk, magnetic tapes, etc. the data which is stored in secondary storage devices is permanent. To store the data we have two approaches. They are using files and using database.

If we store the data permanently in the form of a file, then the data of that file can be modified by any unauthorized user. Hence, industry always recommends not storing the data in the files. Since, files do not provide any security. Hence, industry always recommends storing the data in the form of database. Since, most of the popular databases provide security in the form of user names and passwords.

In order to store the data permanently in the form of files we must use or import a package called java.io.*

  1. Collection of records is known as file. A record is a collection of field values.
  2. A stream is a flow of data between primary memory and secondary memory either locally (within the system) or globally (across the network) or [A stream is a flow of bytes between primary memory and secondary memory either locally (within the system) or globally (across the network)] or [A stream is a flow of bits between primary memory and secondary memory either locally (within the system) or globally (across the network)].

Types of operations or modeson files:

On files we perform two types of operations they are read mode and write mode. The following diagram will illustrate how to open the file either in read mode or in write mode or in both modes:

Types of streams in JAVA:

Based on transferring the data between primary memory to secondary memory and secondary memory to primary memory. In JAVA we have two types of streams they are byte streams and char streams.

Byte streams are those in which the data will be transferred one byte at a time between primary memory to secondary memory and secondary memory to primary memory either locally or globally. java.io.* package contains some set of classes and interfaces which will transfer one byte at a time.

Hierarchy  chart byte streams

Input Stream class:

This is an abstract class; hence we cannot create an object of this class directly. This class is basically used to open the file in read mode. In this class we have the following methods:

1.	public int read ();
2.	public int length (); //
3.	public int available ();
4.	public void close ();

total size of the file

// available number of bytes only

In JAVA end of file (EOF) is indicated by -1.

Output Stream class:

This is also an abstract class; hence we cannot create an object of this class directly. This class is used for opening the file in write mode. In this class we have the following methods:

1.	public void write (int);
2.	public int length ();
3.	public void available ();
4.	public void close ();