Java Randomaccessfile Class - Java Tutorial

Java Randomaccessfile Class

BLUF: Mastering Java Randomaccessfile Class is a key requirement for any Java developer. This lesson breaks down the object-oriented principles and syntax required to use this concept in real-world applications.
Write Once, Run Anywhere Tip: Java Randomaccessfile Class

Java's versatility is unmatched. Learn how Java Randomaccessfile Class fits into the Java ecosystem and improves your code structure in the tutorial below.

The purpose of this class is to handle input and output operations on a random access file, which functions similarly to a sizable byte array. Within this file, there exists a cursor referred to as the file pointer, which dictates where read and write operations occur. When the end of the file is encountered before reading the intended number of bytes, an EOFException is raised. This exception belongs to the IOException category.

Constructor

Constructor Description
RandomAccessFile(File file,Stringmode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

Method

Modifier and Type Method Method
void close() It closes this random access file stream and releases any system resources associated with the stream.
FileChannel getChannel() It returns the uniqueFileChannelobject associated with this file.
int readInt() It reads a signed 32-bit integer from this file.
String readUTF() It reads in a string from this file.
void seek(long pos) It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
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.
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.
void write(int b) It writes the specified byte to this file.
int read() It reads a byte of data from this file.
long length() It returns the length of this file.
void seek(long pos) It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Example

Example

import java.io.IOException;

import java.io.RandomAccessFile;



public class RandomAccessFileExample {

	static final String FILEPATH ="myFile.TXT";

	public static void main(String[] args) {

		try {

			System.out.println(new String(readFromFile(FILEPATH, 0, 18)));

			writeToFile(FILEPATH, "I love my country and my people", 31);

		} catch (IOException e) {

			e.printStackTrace();

		}

	}

	private static byte[] readFromFile(String filePath, int position, int size)

			throws IOException {

		RandomAccessFile file = new RandomAccessFile(filePath, "r");

		file.seek(position);

		byte[] bytes = new byte[size];

		file.read(bytes);

		file.close();

		return bytes;

	}

	private static void writeToFile(String filePath, String data, int position)

			throws IOException {

		RandomAccessFile file = new RandomAccessFile(filePath, "rw");

		file.seek(position);

		file.write(data.getBytes());

		file.close();

	}

}

The file named myFile.TXT includes the following text: "This class is utilized for both reading from and writing to a random access file."

after running the program it will contains

This class is utilized for the purpose of reading and manipulating text data.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience