Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Edit a file

Reply
Thread Tools

Edit a file

 
 
Rebecca
Guest
Posts: n/a
 
      07-26-2005
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.

 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      07-27-2005
In article <(E-Mail Removed) .com>,
"Rebecca" <(E-Mail Removed)> wrote:

> 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

--
John
jmatthews at wright dot edu
www dot wright dot edu/~john.matthews/
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      07-27-2005
Rebecca wrote:
> 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
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView edit validation, edit dropdown list, delete popup confirm =?Utf-8?B?a2Vu?= ASP .Net 1 01-23-2006 12:51 PM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Edit All Function for DataGrid, and Moving the Edit Function in a DataGrid Schultz ASP .Net 3 02-14-2005 04:47 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments