By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Edit a file using RandomAccessFile

dmjpro
100+
P: 2,476
Hi All ....
I am trying to edit a file using RandomAccessFile.
I am having a little bit problem.

My file is ...
Expand|Select|Wrap|Line Numbers
  1. i m debasis jana.
  2.  
Expand|Select|Wrap|Line Numbers
  1. RandomAccessFile file = new RandomAccessFile("file_name","rw");
  2. file.seek(10);
  3. file.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".getBytes());
  4.  
My edited file is ....
Expand|Select|Wrap|Line Numbers
  1. i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  2.  
Where as I expected as .......
Expand|Select|Wrap|Line Numbers
  1. i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaais jana.
  2.  
Please help me.
I can do this by saving the last file content.
But I want this by using a simple API.

Debasis Jana.
Feb 19 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 785
To my knowledge, randomAccess file does not mean "inserting" characters, but it just overwrites, starting at the given position.

In your case, if it reaches beyond the file end, it also appends.

if you would insert "xxx" only, you should see:
i m debasxxxjana.

usually, you insert "records" there which all have a fixed size. A record is a chunk of data with a fixed size,f or example a byte array.
So if you want to make a change in a record, you completely overwrite the old one with the new one.

A file can consist of thousands of records. So to make changes, you don't need to read he whole file data (all records), modify the desired one and write back the whole file data (all records), which is done in a sequential way, but you can seek to the desired record position and only overwrite the data there, which is much, much faster. That's why it's called "random access".

so "inserting" is not possible easily; if you want to insert, you must shift all records that are behind the inserted data, which takes a long time.
Because of that, you usually only append new records to the end, or remember the gap of a deleted record and insert the new record there.


Hi All ....
I am trying to edit a file using RandomAccessFile.
I am having a little bit problem.

My file is ...
Expand|Select|Wrap|Line Numbers
  1. i m debasis jana.
  2.  
Expand|Select|Wrap|Line Numbers
  1. RandomAccessFile file = new RandomAccessFile("file_name","rw");
  2. file.seek(10);
  3. file.write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".getBytes());
  4.  
My edited file is ....
Expand|Select|Wrap|Line Numbers
  1. i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  2.  
Where as I expected as .......
Expand|Select|Wrap|Line Numbers
  1. i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaais jana.
  2.  
Please help me.
I can do this by saving the last file content.
But I want this by using a simple API.

Debasis Jana.
Feb 19 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.
Feb 19 '08 #3

dmjpro
100+
P: 2,476
My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.
Thanks for your suggestion.
What do you mean by RAF passes?
One more thing I did solve that problem by using the StringBuffer.
I just wanted to know was there any way to do with RandomAccessFile only?

Debasis Jana
Feb 20 '08 #4

BigDaddyLH
Expert 100+
P: 1,216
Thanks for your suggestion.
What do you mean by RAF passes?
One more thing I did solve that problem by using the StringBuffer.
I just wanted to know was there any way to do with RandomAccessFile only?

Debasis Jana
"lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFile is usually a bad idea. What a little while and you will get a better idea of what to do instead.
Feb 20 '08 #5

dmjpro
100+
P: 2,476
"lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFile is usually a bad idea. What a little while and you will get a better idea of what to do instead.

Hahahaa ............. So funny!

Debasis Jana
Feb 20 '08 #6

Expert 100+
P: 785
My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.
Maybe you should elaborate your "experience", or else people don't understand how you achieved this opinion.

If you have a stand-alone program, and a huge amount of data (hunded of megabytes), it's faster to just store the data in a RAF and care for it yourself than accessing a database and let the database store it for you. Especially if you don't want to install a database or configure it.
This means faster way of storage and less code to write.
Also if you write a database-application, you need RAF, which is used internally in every database.

On the other side, a server can write several megabytes of data in one second, so if your data size is less, it's better to read the whole data in, manipulate it and then write it back.
Then you don't need to care about fixed sizes of your records (you can easily increase/decrease size), no need to care about too many gaps inside the data and reducing them by shifting records around etc. Also if you need to update several hundred records at once, then chasing the hard drive head around is much,much slower than writing the whole file at once. On the other side, if you need to update only one record in a while, then RAF is faster.

Usually it's better to save a huge amount of data in multiple small files than having all data in one file, so a "design change" is better than using RAF.
Just think of data corruption: Losing one small file, containing one record, is better than losing the big file that contains all records.
For example email saved in Thunderbird (all small files) or in Outlook (one big file).
Feb 20 '08 #7

BigDaddyLH
Expert 100+
P: 1,216
Maybe you should elaborate your "experience", or else people don't understand how you achieved this opinion.

If you have a stand-alone program, and a huge amount of data (hunded of megabytes), it's faster to just store the data in a RAF and care for it yourself than accessing a database and let the database store it for you. Especially if you don't want to install a database or configure it.
This means faster way of storage and less code to write.
Also if you write a database-application, you need RAF, which is used internally in every database.

On the other side, a server can write several megabytes of data in one second, so if your data size is less, it's better to read the whole data in, manipulate it and then write it back.
Then you don't need to care about fixed sizes of your records (you can easily increase/decrease size), no need to care about too many gaps inside the data and reducing them by shifting records around etc. Also if you need to update several hundred records at once, then chasing the hard drive head around is much,much slower than writing the whole file at once. On the other side, if you need to update only one record in a while, then RAF is faster.

Usually it's better to save a huge amount of data in multiple small files than having all data in one file, so a "design change" is better than using RAF.
Just think of data corruption: Losing one small file, containing one record, is better than losing the big file that contains all records.
For example email saved in Thunderbird (all small files) or in Outlook (one big file).
As you like... I'll stick with databases.
Feb 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.