All About Class RandomAccessFile in Java

digitization, transformation, binary

Class RandomAccessFile in Java

RandomAccessFile is an important class for I/O manipulation. Instances of this class support both reading and writing to a RandomAccessFile. An application can modify the position in the file at which the next read or write occurs.

The RandomAccessFile class reads data from and writes data to a file. The file is specified using a File object or a String that represents a pathname.

Ads code goes here

RandomAccessFile class allows reading and writing of any bytes, text, and primitive Java data types from or to any specified location in a file. Because this class provides random, rather than sequential, access to files, it is neither a subclass of InputStream nor of OutputStream, but it provides an entirely independent method for reading and writing data from or to files.

RandomAccessFile implements the same interfaces as DataInputStream and DataOutputStream, and thus defines the same methods for reading and writing data as those classes do.

Both constructors take a mode parameter that specifies whether the file is being opened solely for reading, or for reading and writing. Each of the constructors can throw a SecurityException if the application does not have permission to access the specified file using the given mode.

Unlike FileInputStream and FileOutputStream, RandomAccessFile supports random access to the data in the file; The seek() method provides random access to the file–it is used to select the position in the file from which, or to which, data should be read or written. Hence the seek() method allows us to alter the current position of the file pointer to any location in the file. RandomAccessFile implements both the DataInput and DataOutput interfaces, so it supports the reading and writing of all the primitive data types.

READ  How to Resolve NoSuchSessionException in selenium free?

The mode argument to the constructor methods should be “r” for a file that is to be read-only, and “rw” for a file that is to be written and read.

The structure of the class RandomAccessFile is defined as:

public class java.io.RandomAccessFile extends java.lang.Object implements 
                                      java.io.DataOutput,java.io.DataInput{
//constructors
public RandomAccessFile(File file,String mode)throws IOException;
//creates a new RandomAccessFile stream to read from and optionally to write to,
//the file specified by the file argument.The mode argument must either be equal to 'r'
//or to 'rw' indicating that either to open the file for input or for both input and output respectively.
public RandomAccessFile(String name,String mode)throws IOException;
//creates a new RandomAccessFile stream to read from and optionally to write to,
//the file specified by the specified argument.The mode argument must either be equal to 'r'
//or to 'rw' indicating that either to open the file for input or for both input and output respectively.
//Methods
public native void close() throws IOException;
public native  int read() throws IOException;
public final FileDescriptor getFD() throws IOException;
public native long getFilePointer() throws IOException;
public native long length() throws IOException;
public int read(byte b[]) throws IOException;
public int read(byte b[],int offSet,int length) throws IOException;
public final boolean readBoolean() throws IOException;
public final byte readByte() throws IOException;
public final char readChar() throws IOException;
public final double readDouble() throws IOException;
public final float readFloat() throws IOException;
public final void readFully(byte b[]) throws IOException;
public final void readFully(byte b[],int offSet,int length) throws IOException;
public final int readInt() throws IOException;
public final String readLine() throws IOException;
public final long readLong() throws IOException;
public final short readShort() throws IOException;
public final int readUnsignedByte() throws IOException;
public final int readUnsignedShort() throws IOException;
public final String readUTF() throws IOException;
public native void seek(long pos) throws IOException;
public int skipBytes(int n) throws IOException;
public native void write(byte b[]) throws IOException;
public native void write(int i) throws IOException;
public final void writeBoolean(boolean v) throws IOException;
public final void writeByte(int v) throws IOException;
public final void writeBytes(String s) throws IOException;
public final void writeChar(int v) throws IOException;
public final void writeChars(String s) throws IOException;
public final void writeDouble(double d) throws IOException;
public final void writeFloat(float f) throws IOException;
public final void writeInt(int v) throws IOException;
public final void writeLong(long l) throws IOException;
public final void writeShort(int i) throws IOException;
public final void writeUTF(String s) throws IOException;
public void write(byte b[],int offSet,int length) throws IOException;
}

The details description of the methods are as follows

constructors of this class may throw exceptions as given below:
Throws IOException is I/O error occurs
Throws IllegalArgumentException if the mode argument is not equal to ‘r’ or ‘rw’.
Throws SecurityException if a Security Manager Exists, its chackRead() method is called with the pathname of the file argument to see if the application is allowed to read access to the file. If the mode argument equals to’rw’, its checkWrite() method is also called with the pathname to see if the application is allowed to write access to the file.
Regarding methods

  1. All the read methods are blocked until either the byte is read, the end of the stream is detected or an exception is thrown.
  2. All the read methods throw an EOFException if an end of the file encountered.
  3. All the methods in this class throw an IOException if IO errors occur.
READ  Class ParsePosition in Java

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *