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.
John B. Matthews - 27 Jul 2005 03:49 GMT
> 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
[quoted text clipped - 4 lines]
> 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:-)

Signature
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
Thomas Weidenfeller - 27 Jul 2005 09:28 GMT
> 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
[quoted text clipped - 4 lines]
> 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

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/