 
 - Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - RandomAccessFile class
Introduction
The Java RandomAccessFile class file behaves like a large array of bytes stored in the file system.Instances of this class support both reading and writing to a random access file.
Class declaration
Following is the declaration for Java.io.RandomAccessFile class −
public class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable
Class constructors
| Sr.No. | Constructor & Description | 
|---|---|
| 1 | RandomAccessFile(File file, String mode) This creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. | 
| 2 | RandomAccessFile(File file, String mode) This creates a random access file stream to read from, and optionally to write to, a file with the specified name. | 
Class methods
| Sr.No. | Method & Description | 
|---|---|
| 1 | void close() This method Closes this random access file stream and releases any system resources associated with the stream. | 
| 2 | FileChannel getChannel() This method returns the unique FileChannel object associated with this file. | 
| 3 | FileDescriptor getFD() This method returns the opaque file descriptor object associated with this stream. | 
| 4 | long getFilePointer() This method returns the current offset in this file. | 
| 5 | long length() This method returns the length of this file. | 
| 6 | int read() This method reads a byte of data from this file. | 
| 7 | int read(byte[] b) This method reads up to b.length bytes of data from this file into an array of bytes. | 
| 8 | int read(byte[] b, int off, int len) This method reads up to len bytes of data from this file into an array of bytes. | 
| 9 | boolean readBoolean() This method reads a boolean from this file. | 
| 10 | byte readByte() This method reads a signed eight-bit value from this file. | 
| 11 | char readChar() This method reads a character from this file. | 
| 12 | double readDouble() This method reads a double from this file. | 
| 13 | float readFloat() This method reads a float from this file. | 
| 14 | void readFully(byte[] b) This method reads b.length bytes from this file into the byte array, starting at the current file pointer. | 
| 15 | void readFully(byte[] b, int off, int len) This method reads exactly len bytes from this file into the byte array, starting at the current file pointer. | 
| 16 | int readInt() This method reads a signed 32-bit integer from this file. | 
| 17 | String readLine() This method reads the next line of text from this file. | 
| 18 | long readLong() This method reads a signed 64-bit integer from this file. | 
| 19 | short readShort() This method reads a signed 16-bit number from this file. | 
| 20 | int readUnsignedByte() This method reads an unsigned eight-bit number from this file. | 
| 21 | int readUnsignedShort() This method reads an unsigned 16-bit number from this file. | 
| 22 | String readUTF() This method reads in a string from this file. | 
| 23 | void seek(long pos) This method sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. | 
| 24 | void setLength(long newLength) This method sets the length of this file. | 
| 25 | int skipBytes(int n) This method attempts to skip over n bytes of input discarding the skipped bytes. | 
| 26 | void write(byte[] b) This method writes b.length bytes from the specified byte array to this file, starting at the current file pointer. | 
| 27 | void write(byte[] b, int off, int len) This method writes len bytes from the specified byte array starting at offset off to this file. | 
| 28 | void write(int b) This method writes the specified byte to this file. | 
| 29 | void writeBoolean(boolean v) This method writes a boolean to the file as a one-byte value. | 
| 30 | void writeByte(int v) This method writes a byte to the file as a one-byte value. | 
| 31 | void writeBytes(String s) This method writes the string to the file as a sequence of bytes. | 
| 32 | void writeChar(int v) This method writes a char to the file as a two-byte value, high byte first. | 
| 33 | void writeChars(String s) This method writes a string to the file as a sequence of characters. | 
| 34 | void writeDouble(double v) This method converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first. | 
| 35 | void writeFloat(float v) This method converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first. | 
| 36 | void writeInt(int v) This method writes an int to the file as four bytes, high byte first. | 
| 37 | void writeLong(long v) This method writes a long to the file as eight bytes, high byte first. | 
| 38 | void writeShort(int v) This method writes a short to the file as two bytes, high byte first. | 
| 39 | void writeUTF(String str) This method writes a string to the file using modified UTF-8 encoding in a machine-independent manner. | 
Methods inherited
This class inherits methods from the following classes −
- Java.io.Object
Example - Properly closing a RandomAccessFile after writing
The following example shows the usage of RandomAccessFile close() method.
RandomAccessFileDemo.java
 package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("example1.txt", "rw"); raf.writeUTF("Hello, RandomAccessFile!"); System.out.println("Data written to file."); raf.close(); // Closes the file properly System.out.println("File closed successfully."); } catch (IOException e) { e.printStackTrace(); } } }  Output
Let us compile and run the above program, this will produce the following result−
Data written to file. File closed successfully.
Explanation
- A file named example1.txt is created (or opened), and data is written. 
- close() ensures all data is flushed to disk and the file is no longer in use. 
- Best practice to prevent resource leaks. 
Example - Positioning the file pointer using FileChannel.position()
The following example shows the usage of RandomAccessFile getChannel() method.
RandomAccessFileDemo.java
 package com.tutorialspoint; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.io.IOException; import java.nio.ByteBuffer; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("example2.txt", "rw")) { raf.writeUTF("1234567890"); FileChannel channel = raf.getChannel(); channel.position(2); // Move to 2nd byte (inside UTF string) ByteBuffer buffer = ByteBuffer.allocate(8); int bytesRead = channel.read(buffer); // Read 8 bytes from position 2 buffer.flip(); System.out.print("Data read from position 2: "); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } } catch (IOException e) { e.printStackTrace(); } } }  Output
Let us compile and run the above program, this will produce the following result−
Data read from position 2: 12345678
Explanation
- Writes data using RandomAccessFile. 
- Uses the FileChannel to move the file pointer to a specific position and read from there using a ByteBuffer. 
- Demonstrates how to perform non-sequential (random) file access with getChannel(). 
Example - Reading a specific byte using seek() and read()
The following example shows the usage of RandomAccessFile read() method.
RandomAccessFileDemo.java
 package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("read_example2.txt", "rw")) { raf.writeBytes("1234567890"); raf.seek(5); // Move file pointer to byte index 5 int data = raf.read(); System.out.println("Byte at position 5: " + data + " (char: " + (char) data + ")"); } catch (IOException e) { e.printStackTrace(); } } }  Output
Let us compile and run the above program, this will produce the following result−
Byte at position 5: 54 (char: 6)
Explanation
- Writes digits 1−0 into the file. 
- Seeks to byte index 5 (i.e., 6th character). 
- Reads and prints the character at that position ('6').