A random-access file is a file in which the user or programmer can read and write data to the file at any location (hence the name random) simultaneously. A random-access file behaves like a large array of bytes. To navigate through the file, there is a file pointer which can be moved to the desired location.
The RandomAccessFile in Java is a pre-defined class that belongs to java.io package which allows the programmers to work with random access files. While moving the file pointer, if end of file is reached, it throws an EOFException. The constructors in RandomAccessFile class are given below:
Constructor | Description |
RandomAccessFile(File file, String mode) | Creates a random-access file stream for reading and writing to the file based on the specified mode. |
RandomAccessFile(String string, String mode) | Creates a random-access file stream for reading and writing to the file based on the specified file name and mode. |
Some of the methods available in the RandomAccessFile class are given below:
Method | Description |
void close() | Closes the random-access file stream and releases any resources associated with it |
int read | Reads a byte of data from the file |
int readInt() | Reads a signed 32-bit integer from the file |
String readUTF() | Reads a string from the file |
void seek(long pos) | Sets the file pointer offset from the beginning of the file |
void write(int b) | Writes a specified byte to the file |
long length() | Returns the length of the file |
Following Java program demonstrates reading and writing to a random-access file using the RandomAccessFile class:
import java.io.*;
public class RandomAccess
{
public static void main(String[] args) throws Exception
{
RandomAccessFile raf = new RandomAccessFile("rand.txt", "rw");
int size = (int)raf.length();
System.out.println("File length = " + size + " bytes.");
//Code for reading data from the file
raf.seek(10);
byte[] data = new byte[size];
raf.read(data);
System.out.println("Content in the file = " + new String(data));
//Code for writing data to the file
raf.seek(size);
String s = "New piece of text.";
raf.write(s.getBytes());
raf.close();
}
}
Output for the above program is:
File length = 30 bytes.
Content in the file = sample line of text.
When you open rand.txt and see the content, it will be:
This is a sample line of text.New piece of text.
For more information, visit the following links:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply