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:
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!...
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:
- "r" : The file is opened in a read-only mode. You will receive an IOException if you attempt to write to the file.
- "rw" : The file is opened in a read-write mode. The file is created if it does not exist.
- "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.
- "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!...
A easy and exciting blog about java learning. Please comment your opinions and share..
ReplyDeletehttp://foundjava.blogspot.in
ReplyDeleteThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic.
Pawn Shop
Pawn Loans
Pawn Shops
Pawn Loan
Pawn Shop near me
Thank you for sharing your information. I really appreciate your efforts and I am waiting for your further post thanks once again.
ReplyDeleteRegards,
Java Online Training
Java Online Training in India
Java Online Training India
Java Online Training in Hyderabad
Java Online Training Hyderabad
Java Training in Hyderabad
Java Training in India
Java Training Institutes in India
Java Training Institutes in Hyderabad
Java Course in Hyderabad
COEPD LLC - Center of Excellence for Professional Development is the most trusted online training platform to global participants. We are primarily a community of Business Analysts who have taken the initiative to facilitate professionals of IT or Non IT background with the finest quality training. Our trainings are delivered through interactive mode with illustrative scenarios, activities and case studies to help learners start a successful career. We impart knowledge keeping in view of the challenging situations individuals will face in the real time, so that they can handle their job deliverables with at most confidence.
ReplyDeletecoepd.us/index.html
Java is highly professional course, and it helps students.Thanks for sharingJavaOnline Training Hyderabad
ReplyDeleteThanks for the post, I am techno savvy. I believe you hit the nail right on the head.
ReplyDeleteI am highly impressed with your blog. It is very nicely explained.
Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.
Best MCA
ReplyDeletecolleges in noida
Best MSC IT
colleges in noida
Best M
TECH colleges in noida
It is only through informative blogs like this that we will be able to understand lots of inner concepts of digital marketing.
ReplyDeleteSelenium Training In Chennai
Java Training In Chennai
nice post..best .net training institute in chennai
ReplyDelete.net training
dot net training institute
advanced .net training in chennai
advanced dot net training in chennai
ms office training in chennai
ms office training institute in chennai
ms sql server training in chennai
best sql server training institute
mysql dba training institute
mysql dba training in chennai
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeleteDot Net Classes in Chennai
Selenium Training in Chennai | Selenium Training Institute In Chennai
Best Software Testing Training Institute In Chennai | Manual Software Testing Coaching institute in Chennai | Testing Courses in Chennai
Java Training Institute in Chennai | Core Java Training in Chennai | Java Course and Certification
PHP Course in Chennai
All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information,
ReplyDeleteI love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
Java training in Btm layout
Java training in Jaya nagar
Java training in Electronic city
Java training in Chennai
Java training in USA