Java-RandomAccessFile

RandomAccessFile

When we read the contents of a file, we normally start at the beginning of the file, read the entire contents until we reach the end of the file. This access pattern is called sequential access. Sometimes, we will like to access specific locations in a file and change the data at just those locations. This access pattern is known as random access. Here, the word “random” means that we can read and modify any byte stored at any location in a file.

The RandomAccessFile class enables us to randomly access any location in a file. We can read and write bytes, characters and primitive data types to any location in a file. This class supports a pointer known as the file pointer. It points to a position in a random access file. The seek( ) method in this class is used to move the file pointer to a desired position. The RandomAccessFile is the only bi-directional stream in Java. A file can be opened for random access for only reading or for both reading and writing by specifying one of the two modes “r” or “rw” in the constructor of the RandomAccessFile class.

RandomAccessFile Constructors

Constructor Description
RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

RandomAccessFile Methods

Method Description
void write(int b) This method used to write the specified byte(int to byte) to the file stream.
void write(byte[] b) This method used to write b.length bytes from the specified byte array to the file stream.
void write(byte[] b, int off, int len) This method used to write len bytes from the specified byte array starting at offset off to the file stream.
void writeBoolean(boolean v) This method used to write the specified boolean to the file stream as a 1-byte value.
void writeByte(int v) This method used to write the specified byte(int to byte) to the file stream.
void writeBytes(String s) This method used to write the specified string to the file stream as a sequence of bytes.
void writeChar(int v) This method used to write the specified char to the file stream as a 2-byte value, high byte first.
void writeChars(String s) This method used to write the specified string to the file stream as a sequence of characters.
void writeInt(int v) This method used to write the specified int to the file stream as four bytes, high byte first.
void writeFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file stream as a 4-byte quantity, high byte first.
void writeDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file stream as an 8-byte quantity, high byte first.
void writeLong(long v) This method used to write the specified long to the file stream as eight bytes, high byte first.
void writeShort(int v) This method used to write the specified short to the file stream as two bytes, high byte first.
void writeUTF(String str) This method used to write the specified string to the file stream using modified UTF-8 encoding in a machine-independent manner.
int           read() Reads a byte of data from the file stream.
int           read(byte[] b) Reads up to b.length bytes of data from the file stream into an array of bytes.
int           read(byte[] b, int off, int len) Reads up to len bytes of data from the file stream into an array of bytes.
boolean  readBoolean() Reads a boolean data from the file stream as a 1-byte value.
byte        readByte() Reads a byte of data from the file stream.
char        readChar() Reads a character data from the file stream.
double    readDouble() Reads a double data from the file stream.
float        readFloat() Reads a float data from the file stream.
int           readInt() Reads a integer data from the file stream.
long        readLong() Reads a long data from the file stream.
short       readShort() Reads a short data from the file stream.
int           readUnsignedByte() Reads a unsigned byte from the file stream.
int           readUnsignedShort() Reads a unsigned short from the file stream.
String     readUTF() Reads a String in modified UTF-8 format from the file stream.
String     readLine() Reads a next line of text from the file stream.
void        readFully(byte[] b) Reads up to b.length bytes of data from the file stream into an array of bytes.
void        readFully(byte[] b, int off, int len) Reads up to len bytes of data from the file stream into an array of bytes.
FileChannel     getChannel() Returns the file channel object associated with this file stream.
FileDescriptor  getFD() Returns the file descriptor associated with this file stream.
long                 getFilePointer() Returns the current offset in this file.
long                 length() Returns the length of this file.
void                 seek(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
int                    skipBytes(int n) Skips over and discards n bytes of data from the file stream.
void                 setLength(long newLength) Sets the length of this file.
void                 close() Closes the stream and releases any system resources associated with it.

Program

random

Program Source

import java.io.RandomAccessFile;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        try(RandomAccessFile raf = new RandomAccessFile("rand.da","rw")){
            raf.writeShort(20);
            raf.writeInt(40);
            raf.writeShort(60);
            raf.writeInt(80);
            raf.seek(0);
            System.out.println(raf.readShort());
            System.out.println(raf.readInt());
            System.out.println(raf.readShort());
            System.out.println(raf.readInt());
            raf.seek(2);
            System.out.println(raf.readInt());
            raf.writeShort(100);
            raf.seek(6);
            System.out.println(raf.readShort());
            System.out.println(raf.readInt());
            
        }catch(IOException ex){
            
            System.out.println("An I/O Error Occurred");
        }
    }
}

Leave a Comment