☰ Topics

File Handling

File Operations: There are two specialised classes which perform file read and write operations for a text based files.

  1. FileInputStream: It is a specialised class in reading a text based file in forward direction only, i.e. the cursor will point in forward direction, there is no way to read data in backward direction. For this we have to import java.io.*;

  2. FileOutputStream: It is also a specialised class for writing the data into the text-based file. If the file is not created, it will create a new file and if it is already created it will overwrite it. Here data can be written in forward as well as backward direction. For this we also need to import java.io.*;


To read data from file, read() function is used, its return type is byte in integer format. 1 byte is read at a time. It will return ASCII value. After returning the value, it will move the pointer 1 byte forward. So the syntax should be System.out.print(fis.read());
Since, ASCII value won't be understood by the user, so it is converted to character form or we can say that parsing will be done. Conversion from byte to String will take place. For this we write: System.out.print((char)fis.read()); To reach the end of file a function available() is used. It counts available data in the file after the pointer. It reads byte. It opens file in read mode, cursor points in forward direction.

Example of FileInputStream: Program for reading the contents of file.

import java.io.*;
class exRead                               
{
    public static void main(String[] args) throws Exception                                
    {
          FileInputStream fis = new FileInputStream("exRead.java");
          while(fis.available()!=0)
          {
                 System.out.print((char)fis.read());
          }
          fis.close();
    }
}                                                           
                        


write() function read characters and then stores it in a file. Since the write() function will also write data in ASCII value again conversion from String to Byte takes place. For this we have getBytes() function, its return type will be array of bytes. So, we need to catch it in array. It is fully dynamic.

Example of FileOutputStream: Program to create a new file and write data to it.

import java.io.*;
class exRead
{
      public static void main(String[] args) throws Exception
      {
            FileOutputStream fos=new FileOutputStream("abc.txt");
            String s = "This is a file created by the programmer.";
            byte[] b = s.getBytes();
            fos.write(b);
      }
}
                        

Random Access File Class: When we want to perform read and write operations simultaneoulsy on a file then this class is used. It also comes under IO package only. When we perform write operation on a file then text doesn't move forward instead they are overwritten. seek() function is used for travelling the pointer within the file from one location to another. getFD() keeps the description of the file in its pointer along with write permission & index for counting character in file starts with 0. length() function will count the length of specified string or file. There are 4 classes that are used together to perform read and write operation on a file simultaneously.

  1. RandomAccessFile Class
  2. FileOutputStream Class
  3. PrintStream Class
  4. PrintWriter Class


Example of RandomAccessFile: Program to read and write a character in the same file.

import java.io.*;
class exReadWrite
{
    public static void main(String[] args) throws Exception
    {
          RandomAccessFile raf = new RandomAccessFile("abc.txt","rw");
          PrintStream ps = new PrintStream(new FileOutputStream(raf.getFD()));
          raf.seek(4);     //To move the cursor to 4th position
          ps.println("was");
          raf.close();
    }
}