• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to replace data in a file

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
Can any body please say that how to replace a value or line in a file. For example i have a file with contents
1
2
3
4
5

I need to replace 3 with any value say "Name". How can i do that someone please help.

Thanks and Regards
alexander
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way: read the file into memory (like a String), then replace, then write back.

There is a harder way using RandomAccessFile, shifting bits in the file and then writing the "Name", but you need to know the exact location of the "3", in bytes.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rob.
The above is just an example actual file contains 100000 lines so how could i replace the text. One way i thought is to find the text then we can get the line number using that line number i want to replace the whole line with other text. So how can i do that.Hope you got me.




Thanks and Regards
alexander
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is then use a temporary file. In pseudo code:


If this is not possible, the only option is to use RandomAccessFile. You will need to scan for the line to replace first. Then, shift every single byte after that line up with the difference in line length, then write the new line at the location of the old line. I once wrote a method for the replacing:
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob.
Can i know that RandomAccessFile is used to create files also and can it also be used to read data from normal text files. My data is stored in normal text files. So i need to just edit a particular line isnt there any simple methods to edit the files. The problem is that when ever i try to edit a file and write something it only write at the end of file. How can write the data in middle of file.

As you said i tried using this


File file = new File("myFile");
File tmp = File.createTempFile("myFile", "tmp", file.getParentFile());
BufferedReader reader = new BufferedReader(new FileReader(file));
try
{
PrintWriter writer = new PrintWriter(new FileWriter(tmp);
try
{
String line;
while ((line = reader.readLine()) != null)
{
if (line.equals("3"))
{
line = "Name";
}
writer.println(line);
}
}
finally
{
writer.close();
}
}
finally
{
reader.close();
}
file.delete();
tmp.renameTo(file);


But that doent work

Thanks and Regards
alexander
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter will not work because it doesn't have any methods to seek or read to a certain place in the file. A quick look at its methods should show that.

The suggestion to use RandomAccessFile is what you need to look up. Go to the API Docs, and see if it can both read and write, and also seek to a certain point in the file. This tutorial talks specifically about the class and this tutorial goes over another method.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RandomAccessFile is not really good for reading / writing text files. I tried the following:
However, the first call to the readLine() method already reads the entire channel; therefore, you can't get the actual position anymore.

I tried replacing the loop body as follows:
This is close but since line no longer contains \r or \n you are missing those. The problem is, you don't know whether readLine() stripped off \r, \n or \r\n.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob.

This means that its not possible to just read a file using BufferedReader(new FileReader) and verify a particular line and then write the value on that line using BufferedWriter(new FileWriter). So its just impossible to do with normal text files also, and i have to use RandomAccessFile for doing that kind of work. Shall i understand this. Is it wright?

thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adeeb alexander wrote:This means that its not possible to just read a file using BufferedReader(new FileReader) and verify a particular line and then write the value on that line using BufferedWriter(new FileWriter).

It's perfectly possible to do that, but you'll have to write the complete file, and you you'll have to create a new file while doing so (and deleting the old file and renaming the new file after you're done).

i have to use RandomAccessFile for doing that kind of work.

Note that RandomAccessFile can only be used if the text to be replaced is exactly as long as the text you're replacing it with - the size of the file can't change.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to my requirement the data which has to be replaced will be larger than the old one. Then i have to use the first method only for the work to be done that is


It's perfectly possible to do that, but you'll have to write the complete file, and you you'll have to create a new file while doing so (and deleting the old file and renaming the new file after you're done).


Then this is the the only method for the work to be done. Can i expect any other method. Please give me a simple example. When i have tried to write the contents in a temFile and rename it. That one dint help me. I need a simple example.


Thanks and Regards
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, exactly, do you have so far? What issue, exactly, are you facing implementing the missing pieces?
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Note that RandomAccessFile can only be used if the text to be replaced is exactly as long as the text you're replacing it with - the size of the file can't change.

Well, my example code has shown otherwise
But yes, out-of-the box it doesn't; it requires shifting everything after the replaced data up or down - and that can take a while. And it's not really good for text files unless you know the offsets of pieces of text. And that is the problem I faced before.

To be honest, I haven't ever tried using a Reader/InputStream and Writer/OutputStream on the same file at the same time. Perhaps it will go right, but I see potential problems that the Writer/OutputStream will overwrite data that isn't read yet.


I see only one option here: use a temporary file:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!