Mega Code Archive

 
Categories / Java Tutorial / File
 

RandomAccessFile Introduction

A RandomAccessFile employs an internal pointer that points to the next byte to read. This pointer is zero-based and the first byte is indicated by index 0. When first created, a RandomAccessFile points to the first byte. You can change the pointer's position by invoking the seek method. The skipBytes method moves the pointer by the specified number of bytes. If skipping offset number of bytes would pass the end of file, the internal pointer will only move to as much as the end of file. The skipBytes method returns the actual number of bytes skipped. When opening a file using a RandomAccessFile, you can choose whether to open it read-only or read write. public RandomAccessFile (File file, String mode) throws FileNotFoundException public RandomAccessFile (String filePath, String mode) throws FileNotFoundException The value of mode can be one of these: "r".     Open for reading only.       "rw".    Open for reading and writing.                 If the file does not already exist, RandomAccessFile creates the file.       "rws".   Open for reading and writing and require that every update to the file's content and metadata be written synchronously.       "rwd".   Open for reading and writing and require that every update to the file's content (but not metadata) be written synchronously.