• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace a string in a file.

 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I start my program I write in a file
dateTime;Incomplete;someOther information.
After the "Program finishes execution" I want to reaplce the same line as
dateTime;complete;Some Other Information.
How do i replace the existing line in a file??
(the reason for doing this to let user know whether the program execution completed .if Incompleted s/he will restart).
Thanks in advance
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About your only choice is to read the file, change lines that need changed in String variables and rewrite the file. This seems like a pain but it's not that bad. If you had to make a lot of these thingies we could do a fairly sophisticated design to simplify things, but for one or two I wouldn't bother.

Here's a scheme I like:

This gives you a safe backup and a minimal window of time when the original file is not readable.

Is that useful?
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kind of tried to follow your scheme.

464748950,Mon Mar 21 10:07:46 EST 2005,Incomplete,
-742962523,Mon Mar 21 10:07:48 EST 2005,Incomplete,
-2110719148,Mon Mar 21 10:52:55 EST 2005,Incomplete,
-1904078340,Mon Mar 21 10:53:08 EST 2005,Incomplete,
606747009,Mon Mar 21 10:54:52 EST 2005,Incomplete,
1060394966,Mon Mar 21 10:57:32 EST 2005,Incomplete,
667828495,Mon Mar 21 11:00:22 EST 2005,Complete,
-844176675,Mon Mar 21 11:06:07 EST 2005,Complete,
-504703383,Mon Mar 21 11:12:17 EST 2005,Complete,

the algorithm I followed is like this.

1. At the begining of the execution write(I would say append) the id,date,incomplete to "status.log" file.
2. If the exection is incomplete the file remains the same.
3. If the execution is complete I read the file ("original") ,convert it to a string(temp??), find the exact line based on the id(which is static),replace the status to "Complete", replace the entire line in temp, replace(temp,newString),write it to the file(original).I dont append it now.

It works...But I am just worried about the performance issue(I am reading and writng multiple times) and thinking if there is any more elegant solution.
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!