The following article provides an outline for Java RandomAccessFile. The java RandomAccessFile is a class that is used to read and write for a random access file. The RandomAccessFile class is a built-in class in java that defines the java.io.RandomAccessFile package. The random access file is like an array of bytes stored in a file system. Instances. The RandomAccessFile supports both the read and write operation to the random access file. This file maintains a cursor or pointer; by moving the pointer to a specific position, we can perform the read or write operation to the specific position. The EOF Exception is thrown when the file reaches the end of the file before the required number of bytes has been read.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of RandomAccessFile class in java:
Given below is the declaration syntax of java.io.RandomAccessFile class:
public class RandomAccessFile extends Object implements DataInput,DataOutput, Closeable { // Constructors and methods of the RandomAccessFile class }
The above is the syntax of the RandomAccessFile, where it extends to the Object class and implements to the DataOutput, DataInput and Closeable interfaces.
RandomAccessFileclass member Functions:
TheRandomAccessFileclass contains constructors and some functions as a RandomAccessFile class member function.
Given below are the constructors:
Given below are the functions of the Java FileInputStream Class:
Given below are the examples mentioned:
We create RandomAccessFile objects by using the RandomAccessFile class constructor and pass the file name to read and write a character.
Code:
//package demo; import java.io.IOException; import java.io.RandomAccessFile; public class Demo { public static void main(String[] args) throws Exception { int i; char c; try { // data.txt file contain "Hello!, How are you?" data in it RandomAccessFile f1 = new RandomAccessFile("D:\\data.txt", "r"); i = f1.read(); c = (char) i; System.out.println("The First byte is :" +c); System.out.println("The Number of remaining bytes are :" +f1.length()); // seek method to seek 4 bytes f1.seek(4); i = f1.read(); // converts byte to character c = (char) i; System.out.println("The Next byte after 4 byte seek is :" +c); // getChannel() method System.out.println("\nThe unique FileChannel object is : " +f1.getChannel()); // getFD() method System.out.println("The FileDescriptor object is : " +f1.getFD()); // getFilePointer() method System.out.println("The FilePointer offset is : " +f1.getFilePointer()); RandomAccessFile f2 = new RandomAccessFile("D:\\data.txt", "rw"); //seek 10 bytes and write this message to file f2.seek(10); f2.write("I am good.".getBytes()); f1.close(); f2.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output:
RandomAccessFile class, where we read the data into the byte array.
Code:
//package demo; import java.io.IOException; import java.io.RandomAccessFile; public class Demo { public static void main(String[] args) throws Exception { byte[] b = new byte[16]; int i; char c; try { // data.txt file contain "Hello!, How are you?" data in it RandomAccessFile f1 = new RandomAccessFile("D:\\data.txt", "r"); i = f1.read(b, 1, 15); System.out.println("The total bytes read are : "+i); System.out.print("The bytes read are : "); for(byte t : b) { c = (char)t; System.out.print(c); } f1.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output:
The RandomAccessFile is a class in java that is used to read and write to a random access file. This is a built-in class in java that defines the java.io.RandomAccessFilepackage.
Atas ialah kandungan terperinci Java RandomAccessFile. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!