handling-file-using-RandomAccessFile-class-in-java-with-examples
admin
Read and Write Data in a File Using RandomAccessFile
Java provides multiple ways to handle file operations, and one of the most powerful classes for reading and writing files is RandomAccessFile. Unlike FileReader and FileWriter, RandomAccessFile allows us to read and write at any position within a file, making it useful for database-like applications and binary file handling.
In this tutorial, we'll explore how to use RandomAccessFile in Java, covering:
writeInt(), writeDouble(), writeLong(), and writeBoolean()readBoolean(), readInt(), and readLong()seek() method to navigate within the fileRandomAccessFile in Java?
RandomAccessFile is a Java class that allows us to read and write data to files in a non-sequential order. This means we can move to any location in a file and modify or retrieve data as needed. It supports both read and write operations.
int, double, long, and boolean.RandomAccessFile in Java
The following example demonstrates how to use RandomAccessFile to write and read different types of data in Java.
import java.io.*;
class FileExample7 {
public static void main(String args[]) {
try {
// Create a new file "mydata.dat" in read-write mode
RandomAccessFile rac = new RandomAccessFile("D:/Assignments/mydata.dat", "rw");
// Writing data to file
rac.writeInt(1024); // Writing an integer
rac.writeDouble(456.45); // Writing a double
rac.writeLong(40000); // Writing a long
rac.writeBoolean(true); // Writing a boolean
System.out.println("File size is " + rac.length() + " bytes");
// Moving pointer to the 4th byte to read double value
rac.seek(4);
double value = rac.readDouble();
System.out.println("The double value is " + value);
rac.close(); // Closing file
} catch (Exception ex) {
System.out.println("Error in writing or reading Object");
}
}
}
RandomAccessFile is opened in read-write ("rw") mode.int, double, long, boolean) are written to the file.seek(4) moves the file pointer to a specific byte location to read the double value.seek()
The seek() method allows us to jump to a specific byte position and overwrite existing data. Let’s see an example where we update an existing file.
import java.io.*;
class UpdateFile {
public static void main(String[] args) {
try {
RandomAccessFile file = new RandomAccessFile("D:/Assignments/mydata.dat", "rw");
// Jump to the position of the long value (4 + 8 bytes)
file.seek(12);
// Update long value to 80000
file.writeLong(80000);
System.out.println("File updated successfully");
file.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
seek(12) to move the pointer past the integer (4 bytes) and double (8 bytes).80000 is written, replacing the previous value 40000.RandomAccessFile Over Other File Handling Classes| Feature | RandomAccessFile | FileWriter/FileReader | BufferedReader/BufferedWriter |
|---|---|---|---|
| Read & Write | ✅ Supports both | ❌ Separate classes needed | ❌ Separate classes needed |
| Random Access | ✅ Yes | ❌ No | ❌ No |
| Handles Binary Data | ✅ Yes | ❌ No | ❌ No |
| Performance | ⚡ High for large files | ✅ Good for text | ✅ Good for text |
The RandomAccessFile class in Java provides a powerful way to read and write data at specific locations in a file. Unlike traditional FileReader and FileWriter classes, it allows random access, making it perfect for applications that require frequent updates to a file. By using methods like seek(), writeInt(), writeDouble(), and readLong(), you can efficiently manipulate file content in Java.
RandomAccessFile is ideal for modifying large files.seek() to navigate within the file.int, double, and boolean.
With this knowledge, you can now incorporate RandomAccessFile into your Java projects efficiently! 🚀