473,372 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Edit a file using RandomAccessFile

dmjpro
2,476 2GB
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
7 11021
chaarmann
785 Expert 512MB
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
1,216 Expert 1GB
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
2,476 2GB
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
1,216 Expert 1GB
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
2,476 2GB
"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
chaarmann
785 Expert 512MB
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
1,216 Expert 1GB
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

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

Similar topics

0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
3
by: leila | last post by:
I am new in java. I want to read a simple text file I used this code: import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Date; public class...
3
by: shana07 | last post by:
I have downloaded one image program and about to understand it. This is from its documentation: * image.setInput(in); // in can be InputStream or RandomAccessFile I run the java class from...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
10
by: ioshonowo | last post by:
RandomAccessFile readName = null; RandomAccessFile nameWriter= null; List<String> Names = new ArrayList<String>(); char pan= new char; char cardName= new char; ...
2
by: ioshonowo | last post by:
Hello. I am trying to read the number of lines in a file using this function static int NumberofLines(String file) throws IOException{ RandomAccessFile reader = null; int...
0
by: svpriyan | last post by:
hai, I could not understand how to debug the error i have now. target 1- read the files from directory // i able ot do this part 2- for each file for each file read the content &...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.