This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating a file  RSS feed

 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a file Workorder.txt file, from which i had to modify some values,

Workorder.txt
For eg:
PurchaseNo = 1111
Date = 01/01/2004
Description = New Purchase Order

Now if i to change the Date to 02/02/2004.
Which stream should i use and how.

Actually i posted this in Java section but later found I/O as a separate link.

Thanks,
aakash
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the old string and new string are exactly the same length, you can update a file in place with RandomAccessFile. You can read the file until you find the starting position of the old string then start writing the bytes of the new string at that position. I probably would not take this approach because the next thing somebody asks you to change may not have old and new at exactly the same length.

I guess I'd recommend you read the file, make the changes and write it back out. You might read the whole file into memory so you can browse it beginning to end to look for related bits, or read one line, maybe change it, and write it out. Here's a strategy I've mentioned before:

This gives a lot of safety. You can fail in any step and still have uncorrupted file contents in some form.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!