Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Editing a file

 
vignesh bala
Greenhorn
Posts: 28
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to delete a particular content of a file based on some conditions:

I have to read the file, Check some value in it and delete if its zero.
Can anybody suggest how I can do that
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to to the ranch!

There are some random access techniques to modify a file in place and insert and remove data but it's pretty tricky and it would be hard to show it's worth the effort.

The easier way is to read the input, edit in memory and write a new file. Here you have two options.

One is to read the whole file into memory, maybe a linked list of Strings to represent lines of text. Modify it in memory as much as you like, then write it back out.

The other is to read one line, modify it and write it back out. To omit a given line just don't write it. If you've used Unix you might be familiar with SED or stream editors that do this kind of thing.

Here's an old trick I learned on mainframe time sharing:

This assures you always have a good copy of the original and/or modified data on disk in case you accidentally unplug the computer in mid-program.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!