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
I/O
LEDataStream for little-endian i/o
temporary files
UnifiedIO

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/amend.html J:\mindprod\jgloss\amend.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.212]
You are visitor number 11.