Edit a file

R

Rebecca

I am wondering what the best way to edit a file in Java is. I have a
file that has Header information and then footer information. in
between is the data encircled by Strings like "START DATA" and "END
DATA". I need a file that will contain only the info between these 2
strings. Currently, i am reading in the data with a buffered reader
line by line. extracting the info between thse 2 tags, and creating a
new file with this data. Is this the best way to do something like
this? Is there an easier/more efficient way?
Like is the are way to open the file - remove stuff before start data,
remove stuff after end of data and then close the file. This way i
don't have to write a new file?

Thanks in advance.
 
J

John B. Matthews

Rebecca said:
I am wondering what the best way to edit a file in Java is. I have a
file that has Header information and then footer information. in
between is the data encircled by Strings like "START DATA" and "END
DATA". I need a file that will contain only the info between these 2
strings. Currently, i am reading in the data with a buffered reader
line by line. extracting the info between thse 2 tags, and creating a
new file with this data. Is this the best way to do something like
this? Is there an easier/more efficient way?

This sounds like the right approach, but feel free to post code:)
Like is the are way to open the file - remove stuff before start data,
remove stuff after end of data and then close the file. This way i
don't have to write a new file?

It's hard to resist the temptation to edit files "in place," but you
must. Think of your program as a filter or function that transforms
input into output. You want to keep the old file around until you're
reasonably certain that you're done with it. You can always invoke
renameTo(), delete() or deleteOnExit() when you're sure.
Thanks in advance.

Been there, done that, had to dig out the backup tape:)
 
T

Thomas Weidenfeller

Rebecca said:
I am wondering what the best way to edit a file in Java is. I have a
file that has Header information and then footer information. in
between is the data encircled by Strings like "START DATA" and "END
DATA". I need a file that will contain only the info between these 2
strings. Currently, i am reading in the data with a buffered reader
line by line. extracting the info between thse 2 tags, and creating a
new file with this data. Is this the best way to do something like
this? Is there an easier/more efficient way?

Script it with perl, awk, or another good scripting language. E.g. the
following three lines of awk would do what you want:

/^START DATA/ { f = 1; next }
/^END DATA/ { f = 0; next }
f == 1 { print }

There is no need to fire up a compiler or use a language like Java for
such text processing problems.
Like is the are way to open the file - remove stuff before start data,
remove stuff after end of data and then close the file. This way i
don't have to write a new file?

You could mess around with two RandomAccessFiles in Java. But it is IMHO
not worth the effort at all, and it is risky to perform such file
changes in-place without some proper backup. But if you do a backup of
the file prior to processing, you could also instead just write a new file.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top