Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Updating a file

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic