Net-informations.com
SiteMap  | About    

RandomAccessFile in Java

Java RandomAccessFile providing a nonsequential access to files. It behaves like a large array of bytes stored in the file system. That means you don't need to start from 1st line and you can jump to anywhere in the file . It's similar to array data structure, Just like you can access any element in array by index you can read any content from file by using file pointer. The real advantage is that once file is opened, it can be read from or written to in a random manner just by using a record number or you can add to the end since you will know how many records are in the file.

Moving Around a RandomAccessFile

The RandomAccessFile class allows us to jump to a certain location in the file by using the seek() method. Once the file pointer has been positioned, data can be read from and written to the file using the DataInput and DataOutput interfaces. These interfaces allow us to read and write data in a platform-independent manner. The current position of the file pointer can be obtained by calling the getFilePointer() method.

The above constructor creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Access Mode

In RandomAccessFile, while instantiating default mode is read only . But we can provide different mode. These modes are

Example

Output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.