All About 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 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);
//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);
//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 void close();
public int read();
public final FileDescriptor getFD();
public long getFilePointer();
public long length();
public int read(byte b[]);
public int read(byte b[],int offSet,int length);
public final boolean readBoolean();
public final byte readByte();
public final char readChar();
public final double readDouble();
public final float readFloat();
public final void readFully(byte b[]);
public final void readFully(byte b[],int offSet,int length);
public final int readInt();
public final String readLine();
public final long readLong();
public final short readShort();
public final int readUnsignedByte();
public final int readUnsignedShort();
public final String readUTC();
public void seek(long pos);
public int skipBytes(int n);
public int write(byte b[]);
public void write(int i);
public final void writeBoolean(boolean v);
public final void writeByte(int v);
public final void writeBytes(String s);
public final void writeChar(int v);
public final void writeChars(String s);
public final void writeDouble(double d);
public final void writeFloat(float f);
public final void writeInt(int v);
public final void writeLong(long l);
public final void writeShort(int i);
public final void writeUTF(String s);
public int write(byte b[],int offSet,int length);
}
                                                   
 
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 path mame of the file argument to see if the application is allowed to read access to the file.If the mode argument is 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 stream is detected or an exception is thrown.
  2. All the read methods throe an EOFException if an end of file encountered.
  3. All the methods in this class throw an IOException if IO errors occurs.
All About Class RandomAccessFile in Java All About Class RandomAccessFile in Java Reviewed by Animesh Chatterjee on October 11, 2018 Rating: 5

No comments:

Powered by Blogger.