473,598 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Edit a file using RandomAccessFil e

dmjpro
2,476 Top Contributor
Hi All ....
I am trying to edit a file using RandomAccessFil e.
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 11063
chaarmann
785 Recognized Expert Contributor
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 RandomAccessFil e.
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 Recognized Expert Top Contributor
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 Top Contributor
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 RandomAccessFil e only?

Debasis Jana
Feb 20 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
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 RandomAccessFil e only?

Debasis Jana
"lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFil e 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 Top Contributor
"lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFil e 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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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
3094
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. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
4
3707
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 file via some editable controls such as text boxes , option boxes etc. how can i implment this , should i use another xslt file with <INPUT> controls . if so how can i save the result back using the asp.net
3
3440
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 ForMe { public static void main(String args) { System.out.println( "Testing File name-getting methods." );
3
5371
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 prompt: java Image me.jpg My Q: 1. Am I right by saying that 'me.jpg' is as RandomAccessFile? So what is InputStream here mean?
10
15031
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 without running out of memory, so I'm reading and as I'm reading I'm going to write to a file. This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To...
1
2158
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 I have Categories and various Products in each Category. I'm trying to build a page to EDIT/UPDATE each product.I want to be able to change the Category that a product belongs to, but all I get in the drop-down menu is the one specific Category. ...
10
1923
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; try{ readName= new RandomAccessFile(persoNames + "/perso.txt","rw"); nameWriter= new RandomAccessFile("C:/perso.txt","rw"); for (String line = readName.readLine(); line != null; line =...
2
2158
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 lineCount = 0; try { reader = new RandomAccessFile(file + "/perso.txt", "r"); for (String line = reader.readLine(); line != null; line = reader.readLine()) {
0
3157
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 & compare with the existing List & update the list // i able to do this part when i try to combine both parts , i got some following error
0
7981
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8284
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.