Java - Random access file

Java Conceptuel Diagram

About

A random access file behaves like a large array of bytes stored in the file system.

There is a kind of cursor, or index into the implied array, called the file pointer.

A Random Access File class allows to move around a file and read from it or write to it as you please. You can replace existing parts of a file too.

This is not possible with the FileInputStream or FileOutputStream.

Pointer

To read or write at a specific location, you must first position the file pointer at the desired location

Action Function
Position the pointer seek()
Get the current pointer position getFilePointer()

Operations

Input / Read

Input operations read bytes starting at the file pointer and advance the file pointer past the bytes read.

Output / Write

If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.

Documentation / Reference







Share this page:
Follow us:
Task Runner