Java RandomAccessFile Example

KB Viewed: 6209

Java RandomAccessFile Example

Java provides the RandomAccessFile class, enabling both read and write operations on a file. Unlike other file-handling classes, RandomAccessFile it supports moving to any position within the file to read or write data.

Description

The Java RandomAccessFile class facilitates direct access to a file’s content via file pointer movements. It supports reading and writing to a file, making it versatile for various file manipulation tasks. The class allows reading or writing bytes at specific positions within a file.

Save $100 in the next
5:00 minutes?

Register Here

Example of Java RandomAccessFile

Here’s a simple example demonstrating how to use RandomAccessFile in Java:


import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileExample {
    public static void main (String[] args){

        try {
            // Open or create a new file in read-write mode
            RandomAccessFile file = new RandomAccessFile("example.txt","rw");
            // Write data to the file
            file.writeBytes("Hello, World!");
            // Move the file pointer to a specific position
            file.seek(7); // Move to the 8th byte position
            // Read and print data from the file
            byte[] data = new byte[6]; // Reading 6 bytes
            file.read(data);
            System.out.println("Data at position 8: " + new String(data));
            // Close the file
            file.close();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Let’s Break Down The Code And its Output:

1. Code Explanation

  • Import Statements: Imports necessary classes, including IOException and RandomAccessFile.
  • Class Declaration: Defines a class named RandomAccessFileExample.
  • Main Method: Contains the code execution.

File Operations:

  • Opens a file named “example.txt” in read-write mode using RandomAccessFile.
  • Write the string “Hello, World!” to the file.
  • Moves the file pointer to the 8th-byte position using seek(7).
  • Reads 6 bytes from that position and stores them in the data array.
  • Prints the content read from the file at position 8.
  • Closes the file.

2. Output Explanation

The output should display Data at position 8: World! because the file pointer was moved to the 8th-byte position before reading 6 bytes, starting from there. Thus, it reads “World!” from the file and prints it.

Output:

Output Explanation

Conclusion

The RandomAccessFile class in Java offers flexibility in reading and writing files by permitting direct access to any position within the file. This capability proves particularly useful for scenarios requiring manipulation of specific file sections without reading or writing the entire file.

Using the RandomAccessFile class, developers can efficiently manage file content, making it a powerful tool for various file operations in Java programming.

Save $100 in the next
5:00 minutes?

Register Here