amend a file : Java Glossary


amend a file
There are a number of ways to change the bytes in a file. Here is how to do it, ordered from most to least common.
  1. Read the old file copying to a new file. When you get to the part that should be changed, write new data instead. Then continue copying. When you are done, close both files, delete the old file and rename the new file to the old, Alternatively rename the old file to *.bak or keep both the old and new files. HunkIO. createTempFile helps by creating the temp file in the same directory as the old file where it can be most easily renamed.
  2. You can use the HunkIO. readEntireFile approach and read the whole file into RAM (Random Access Memory), fix it and write it out again in a single I/O.
  3. You can use the append option of FileOutputStream to tack a stream onto the end of an existing file.
  4. You can use RandomAccessFile to patch a few bytes. This technique does not work well when you want to change the length of a chunk of data in the middle of a file. You must leave room for growth when you create the file, or invent some complicated internal file structure the way an SQL engine does to allow records to grow and shrink. If you wanted roll you own, you could implement a hermit crab file. RandomAccessFile is tricky since you have to compute all your own byte offsets.
File I/O Amanuensis to teach you how to do all manner of file, in RAM and remote I/O
LEDataStream for little-endian i/o
temporary files

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number