Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Edit a file

Thread view: 
Rebecca - 26 Jul 2005 16:02 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
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.