JAVA IO CLASSES AND FILE HANDLING



What is Stream?

A stream represents a flow of data or a channel of communication from source to sink.

  • I/O in Java is based on streams.
  • Java 1.0 supports only byte streams.
  • Characters are 16-bit Unicode characters, whereas bytes as always are 8 bits.
  • The InputStream class is the superclass of all of the Java 1.0 byte input streams.
  • OutputStream is the superclass of all the byte output streams.
  • The drawback to these byte streams is that they do not always handle Unicode characters correctly.

The package java.io contains the classes that handle fundamental input and output operations in Java.

Streams The stream classes are for performing I/O on bytes of data.

Readers and Writers The reader and writer classes are for performing I/O on characters.

The I/O classes can be grouped as follows:

  • Output Stream Classes.
  • Input Stream Classes.
  • Writer Class.
  • Reader Class.

Output Stream Class

The OutputStream class is the parent class of all the output streams. It contains five methods:

public void close(). Closes the output stream.

public void flush(). Flushes any buffers.

public void write(int b). Writes a single byte.

public void write(byte [] b). Writes an array of bytes.

public void write(byte [] b, int offset, int length). Writes length number of elements in the array starting at the index specified by offset.

Input Stream Class

For each output stream class, there is a corresponding input stream class for reading in the data. The abstract InputStream class is the parent class of all the input streams, and it contains read() methods that correspond to the write() methods of the OutputStream class. It also contains several other methods, including:

public int read(). Reads a single byte from the stream.

public int read(byte [] b). Reads in a collection of bytes and places them in the given array. The return value is the actual number of bytes read.

public int read(byte [] b, int offset, int length). Reads a collection of bytes into the specified location of the array.

public void close(). Closes the stream.

public long skip(long n). Skips the next n bytes in the stream.

public void mark(int readLimit). Marks the current location of the stream. This method is used in conjunction with the reset() method on streams that allow support for pushback. After readLimit number of bytes are read, the marked location becomes invalid.

public void reset(). Resets the position of the stream to the location of the most recent mark() call, assuming that the marked location is still valid.

public int available(). Returns the number of bytes that can be read from the input stream without blocking.

Input Output Streams Classes

Example

   public class InputDemo 
  {
   public static void main(String[] args) throws Exception {
      
      InputStream st = null;
      int i;
      char c;
      
      try{
         // new input stream created
         st = new FileInputStream("C://myfile.txt");
         
         System.out.println("Read Data:");
         
         // reads till the end of the stream
         while((st=is.read())!=-1)
         {
            // converts integer to character
            c=(char)i;
            
            // prints character
            System.out.print(c);
         }
      }catch(Exception e){
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally{
         
         // releases system resources associated with this stream
         if(st!=null)
            st.close();
      }
   }
}


Say we have a text file c:/myfile.txt, which has the following content.HELLO

Now compile and run the above program, this will produce the following result:

Read Data: HELLO