Java.io - SPLessons

Java.io RandomAccessFile

Home > Lesson > Chapter 44
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io RandomAccessFile

Java.io RandomAccessFile

shape Introduction

The Java.io RandomAccessFile is the class used to access the files randomly. This is additional advantage compared to FileInputStream and FileOutputStream to access the file. The Java API allows the RandomAccessFile class to move arround the file for reading from file or to write into the file.

shape Description

By using seek() method, to position the file pointer for to read or write at that particular location. By calling the getFilePointer() method one can achieve the position of file pointer. By using close() method one can close the random access file.

Class Declaration

shape Declaration

Java.io RandomAccessFile class is declared below. Public class RandomAccessFile extends Object implements DataInput, DataOutput, Closeable 

Class Constructors

shape Table

Constructors Description
RandomAccessFile(File file, String mode) The function of this constructor is to create a random access file with represented file to read or write from.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
void read fully(byte[] b) The function of this method is to read the bytes from the input stream and store in array bytes starting from file pointer.
void write (byte b) The function of this method is to write the represented byte into file from current file pointer.
void write (byte[] b, int off, int n) By using this method it writes the data up to 'n' bytes from the array of bytes starting at current file pointer to file.
FileChannel getChannel  () The function of this method is to return the file channel object associated with the current file.
long getFilePointer() The function of this method is to represent the present offset to the file.
void writeBoolean(boolean b) The function of this method is to write the boolean value to the file.
void seek (long pos) The function of this method is to represent the file pointer offset that where have to start.

Inherited Methods

shape Description

From the following classes, methods are inherited to random access file class.
  • Java.io.Object

shape Examples

Usage of string readUTF() method. [c]import java.io.*; public class RandomAccessFile { public static void main(String[] args) { try { // creating a new RandomAccessFile with filename as test RandomAccessFile rf = new RandomAccessFile("D:/test.txt", "rw"); // To write something in the file rf.writeUTF("SP LESSONS"); // To set the file pointer at 0 position rf.seek(0); // To print the string System.out.println("" + rf.readUTF()); // To set the file pointer at 7 position rf.seek(7); // To write something in the file rf.writeUTF("example"); // To set the file pointer at 0 position rf.seek(0); // To print the string System.out.println("" + rf.readUTF()); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c] SP LESSONS SP LE[/c] Usage of FileChannel getChannel() method [c]import java.io.*; public class RandomAccessFile { public static void main(String[] args) { try { // creating a new RandomAccessFile with filename as test RandomAccessFile rf = new RandomAccessFile("D:/test.txt", "rw"); // To write something in the file rf.writeUTF("SP LESSONS"); // To set the file pointer at 0 position rf.seek(0); // To read and print the contents of the file System.out.println("" + rf.readUTF()); // To return the channel of the file System.out.println("" + rf.getChannel()); // To close the stream and release other resources rf.close(); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c] SP LESSONS sun.nio.ch.FileChannelImpl@2a139a55 [/c]

Summary

shape Key Points

  • The Java.io RandomAccessFile is the class used to access the files randomly.
  • This is additional advantage compared to FileInputStream and FileOutputStream to access the file.
  • The Java API allows the Java.io RandomAccessFile class to move arround the file for reading from file or to write into the file.