Java RandomAccessfile
Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer cursor, which is present in the array, we may do read and write operations. End-of-file exceptions are thrown if they occur before a particular number of bytes are read. It is an IOException subtype.
Java IO includes the class RandomAccessFile. We must specify the method to open the file when creating a RandomAccessFile instance in Java. For instance, we must use "r" to open the file in read-only mode and "rw" to open it in read-write mode. Data from a random access file can be read or written at any position by using a file pointer. You can use the getFilePointer() function to obtain the current file pointer and the seek(int I method to set its file pointer index. Any index where we write data overrides any data that is already there.
Constructor
RandomAccessFile(File file, String mode)
The file provided by the File parameter is produced as a random access file stream and is used to read from and, potentially, write to the file.
RandomAccessFile(String name, String mode)
Creates one random access file stream that read from and, optionally, writes to a file with the supplied name.
Method
1. Modifier type: void
Method : close()
The random access file stream is shut down, and any system resources used by the stream are released.
2. Modifier type: FileChannel
Method: getChannel
It gives back the particular FileChannel object connected to this file.
3. Modifier type: int
Method : readInt();
a signed 32-bit integer is read from the file
4. Modifier type: String
Method : readUTf()
A string is read from this file and entered.
5. Modifier type: void
Method : seek(long pos)
It establishes the file-pointer offset from the file's start where the following read or write will take place.
6. Modifier type: void
Method : writeDouble(double v)
Using the doubleToLongBits function from the Double class, it transforms the double parameter to a long, which is then written to the file as a 8 number, high byte first.
7. Modifier type: void
Method : writeFloat(float v)
The function writeFloat(float v) writes the converted float result to the file as a four-bit number, high byte first, utilizing floatToIntBits method from the class Float.
8. Modifier type: void
Method : write(int b)
It updates this file with the specified byte.
9. Modifier type: int
Method : read()
A byte of information is read from this file.
10. Modifier type: long
Method : length()
It gives back the size of this file.
11. Modifier type: void
Method : seek(long pos)
It establishes the file-pointer offset from the file's start where the following read or write will take place.
Example program
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessfile {
static final String Filepath ="myFile.txt";
public static void main(String[] args) {
try {
System.out.println(new String(readFromFile(FILEPATH, 1, 10)));
writeToFile(Filepath, "Hello people have a good day", 30);
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] readFromFile(String filePath, int pos, int size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(pos);
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 sentence "This class is utilized for read and write to random access file" can be found in the file myFile.txt.
Once the software has been executed, it will contain
Example program
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccess {
public static void main(String[] args) {
try {
String filePath = "/Users/sravani/Downloads/source.txt";
System.out.println(new String(readCharsFromFile(filePath, 4, 6)));
writeData(filePath, "Data", 4);
appendData(filePath, "sravani");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void appendData(String filePath, String data) throws IOException {
RandomAccessFile rFile = new RandomAccessFile(filePath, "rw");
rFile.seek(raFile.length());
System.out.println("current pointer = "+rFile.getFilePointer());
rFile.write(data.getBytes());
rFile.close();
}
private static void writeData(String filePath, String data, int seek) throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(seek);
file.write(data.getBytes());
file.close();
}
private static byte[] readCharsFromFile(String filePath, int seek, int chars) throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(seek);
byte[] bytes = new byte[chars];
file.read(bytes);
file.close();
return bytes;
}
}