Wednesday, July 29, 2015

How to Read and Write from RandomAccessFile in Java

A FileInputStream lets you read data from a file whereas a FileOutputStream lets you write data to a file. A random access file is a combination of both. Using a random access file, you can read from a file as well as write to the file. Reading and writing using the file input and output streams are a sequential process. Using a random access file, you can read or write at any position within the file (hence the name random access). In this article, we'll try to see how to use RandomAccessFile with the help of an example.


An object of the RandomAccessFile class facilitates the random file access. It lets you read/write bytes and all primitive types values to a file. It also lets you work with strings using its readUTF() and writeUTF() methods. The RandomAccessFile class is not in the class hierarchy of the InputStream and OutputStream classes

Access Mode of Random access file
A random access file can be created in four different access modes. In its constructor, you must specify the access mode. The access mode value is a string. They are listed as follows:

  1. "r" : The file is opened in a read-only mode. You will receive an IOException if you attempt to write to the file.
  2. "rw" : The file is opened in a read-write mode. The file is created if it does not exist.
  3. "rws" : Same as the "rw" mode, except that any modifications to the file’s content and its metadata are written to the storage device immediately.
  4. "rwd" : Same as the "rw" mode, except that any modifications to the file’s content are written to the storage device immediately.

You create an instance of the RandomAccessFile class by specifying the file name and the access mode as shown:

RandomAccessFile raf = new RandomAccessFile("randomtest.txt", "rw");


File Pointer
A random access file has a file pointer that is advanced when you read data from it or write data to it. The file pointer is a kind of cursor where your next read or write will start. Its value indicates the distance of the cursor from the beginning of the file in byes. You can get the value of file pointer by using its getFilePointer() method. When you create an object of the RandomAccessFile class, the file pointer is set to zero, which indicates the beginning of the file. You can set the file pointer at a specific location in the file using the seek() method.

Length function
The length() method of a RandomAccessFile returns the current length of the file. You can extend or truncate a file by using its setLength() method. If you extend a file using this method, the contents of the extended portion of the file are not defined.


Reading from and writing to a random access file is performed the same way you have been reading/writing from/to any input and output streams.


Example 
Following example explain the use of a random access file. When you run this program, it writes two things to a file: the file read counter, which keeps track of how many times a file has been read using this program, and a text message of "Hello Java Latte". The program increments the counter value in the file every time it reads the file. The counter value keeps incrementing when you run this program repeatedly. You may get a different output every time you run this program.

I hope this example has given you the brief notion of Random Access File class and how it works.



If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...

No comments:

Post a Comment