Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code to Modify the contents of a file

 
Moni Marva
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The contents of the .log file are as mentioned below,

12/02/2005,12:30:06:09,12/02/2005,12:40:09:09,failure,ind-mhp1dxp0323

In this I want to change the status from say "failure" to "sucess" for a particular line.
I want the solution to do this using File Io , Random Access file.
 
sandip mense
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moni,

These are the steps that you need to follow,

Load the file from the file system.
Open the file in read/write mode.
Read the line into a StringBuffer.
Get the length of the string that you want to replace
Replace the String using the StringBuffer replace(startindex, endindex, Stringtoreplace) method

Sandip
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With RandomAccess you can locate a string in a file and overwrite it with something else of exactly the same length. Since "failure" and "success" both have 7 letters you could do it in place. The "locate" part might be a little challenging, though, since there's no API to read one line at a time from RandomAccess.

With ordinary file io you can read the input one line at a time, examine each line, make the change when you find the target, and write a new file one line at a time. I like this trick to be very safe when rewriting a file:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic