Java - Random access file

> Procedural Languages > Java

1 - 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.

3 - 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()
Advertising

4 - Operations

4.1 - Input / Read

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

4.2 - 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.

5 - Documentation / Reference

lang/java/randomaccessfile.txt · Last modified: 2017/09/06 09:15 by gerardnico