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

how to change the content of a file

 
mythily prakash
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I want to change the content of a file,not all the contents but some slite modifications to the file.Can anyone help me to get a code to do this?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need some more details - for example, what is the file name (or would you prefer to specify it on the command line), what is the type of file and its contents (e.g. plain text, XML, binary, compressed archive) and what sort of changes do you need to make?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, you can overwrite bytes in the middle of a file, but you can't easily replace old stuff with new stuff of a different length. So change and append are easy, but insert and delete are tough. It's common to read all of the old file and write a totally new one with the same or changed content.

See RandomAccessFile if you can be sure the data length doesn't change. Otherwise just read and write with whatever IO works with your data.
[ March 16, 2006: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic