The perfect place for easy learning...

Java Programming

×

Topics List


RandomAccessFile in Java





In java, the java.io package has a built-in class RandomAccessFile that enables a file to be accessed randomly. The RandomAccessFile class has several methods used to move the cursor position in a file.

A random access file behaves like a large array of bytes stored in a file.

RandomAccessFile Constructors

The RandomAccessFile class in java has the following constructors.

S.No. Constructor with Description
1 RandomAccessFile(File fileName, String mode)

It creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

2 RandomAccessFile(String fileName, String mode)

It creates a random access file stream to read from, and optionally to write to, a file with the specified fileName.

Access Modes

Using the RandomAccessFile, a file may created in th following modes.

  • r - Creates the file with read mode; Calling write methods will result in an IOException.
  • rw - Creates the file with read and write mode.
  • rwd - Creates the file with read and write mode - synchronously. All updates to file content is written to the disk synchronously.
  • rws - Creates the file with read and write mode - synchronously. All updates to file content or meta data is written to the disk synchronously.

RandomAccessFile methods

The RandomAccessFile class in java has the following methods.

S.No. Methods with Description
1 int read()

It reads byte of data from a file. The byte is returned as an integer in the range 0-255.

2 int read(byte[] b)

It reads byte of data from file upto b.length, -1 if end of file is reached.

3 int read(byte[] b, int offset, int len)

It reads bytes initialising from offset position upto b.length from the buffer.

4 boolean readBoolean()

It reads a boolean value from from the file.

5 byte readByte()

It reads signed eight-bit value from file.

6 char readChar()

It reads a character value from file.

7 double readDouble()

It reads a double value from file.

8 float readFloat()

It reads a float value from file.

9 long readLong()

It reads a long value from file.

10 int readInt()

It reads a integer value from file.

11 void readFully(byte[] b)

It reads bytes initialising from offset position upto b.length from the buffer.

12 void readFully(byte[] b, int offset, int len)

It reads bytes initialising from offset position upto b.length from the buffer.

13 String readUTF()

t reads in a string from the file.

14 void seek(long pos)

It sets the file-pointer(cursor) measured from the beginning of the file, at which the next read or write occurs.

15 long length()

It returns the length of the file.

16 void write(int b)

It writes the specified byte to the file from the current cursor position.

17 void writeFloat(float v)

It converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first.

18 void writeDouble(double v)

It converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.

Let's look at the following example program.

Example
import java.io.*; 

public class RandomAccessFileDemo 
{ 
    public static void main(String[] args) 
    { 
        try
        { 
            double d = 1.5; 
            float f = 14.56f; 
  
            // Creating a new RandomAccessFile - "F2" 
            RandomAccessFile f_ref = new RandomAccessFile("C:\\Raja\\Input-File.txt", "rw"); 
  
            // Writing to file 
            f_ref.writeUTF("Hello, Good Morning!"); 
  
            // File Pointer at index position - 0 
            f_ref.seek(0); 
  
            // read() method : 
            System.out.println("Use of read() method : " + f_ref.read()); 
  
            f_ref.seek(0); 
  
            byte[] b = {1, 2, 3}; 
              
            // Use of .read(byte[] b) method : 
            System.out.println("Use of .read(byte[] b) : " + f_ref.read(b)); 
  
            // readBoolean() method : 
            System.out.println("Use of readBoolean() : " + f_ref.readBoolean()); 
  
            // readByte() method : 
            System.out.println("Use of readByte() : " + f_ref.readByte()); 
  
            f_ref.writeChar('c'); 
            f_ref.seek(0); 
  
            // readChar() : 
            System.out.println("Use of readChar() : " + f_ref.readChar()); 
  
            f_ref.seek(0); 
            f_ref.writeDouble(d); 
            f_ref.seek(0); 
  
            // read double 
            System.out.println("Use of readDouble() : " + f_ref.readDouble()); 
  
            f_ref.seek(0); 
            f_ref.writeFloat(f); 
            f_ref.seek(0); 
  
            // readFloat() : 
            System.out.println("Use of readFloat() : " + f_ref.readFloat()); 
  
            f_ref.seek(0); 
            // Create array upto geek.length 
            byte[] arr = new byte[(int) f_ref.length()]; 
            // readFully() : 
            f_ref.readFully(arr); 
              
            String str1 = new String(arr); 
            System.out.println("Use of readFully() : " + str1); 
  
            f_ref.seek(0); 
              
            // readFully(byte[] b, int off, int len) : 
            f_ref.readFully(arr, 0, 8); 
              
            String str2 = new String(arr); 
            System.out.println("Use of readFully(byte[] b, int off, int len) : " + str2); 
        } 
        catch (IOException ex) 
        { 
            System.out.println("Something went Wrong"); 
            ex.printStackTrace(); 
        } 
    } 
}

When we run the above program, it produce the following output.

RandomAccessFile class in java