This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to replace a line in a file

 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do we replace a line in a file......

I have a if(condition)

When reading a file ,if the condition is true replace a line in the file

else

continue....



Regards
 
John Melton
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem with your current code is you are trying to overwrite the file every time.
Essentially, here's the steps you'll need to take
1. Open the input file A
2. Create an output file B
3. Loop over every line in A
a. If it DOES NOT start with "Page", write the current line to file B
b. If it DOES start with "Page", create your string buffer and write that to file B instead of the current line.

This should work based on your current design. You could do some performance tweaks to make it work better, but this should get you there.
Hope this helps.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This FAQ Page has some more hints about safely replacing an existing file.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic