Re: Modifying a file w/o creating a temp file
- From: Bernie Ohls <bohls@xxxxxxxx>
- Date: Fri, 23 May 2008 19:47:19 -0400
Eric Sosman wrote:
My advice is to stick with the temporary file: The
code will be far simpler, you won't need to worry about
length-changing character encodings, and the program is
likely to run faster (except in the equal-length case or
the contract-with-padding case), because it won't have to
do all that jumping around and sliding back and forth.
Of course there's one simple and obvious solution which has not yet been mentioned or rejected: read the whole file into memory, modify it there, truncate the original file, and write the modified data into it.
This has substantial subtle benefits, balanced against the one flaw of being memory hungry and limited by swap space. Benefits include (a) ease of use, (b) if the original file had hard links to it, they are not broken, (c) if it had special modes/attributes/ACLs, they are not lost, and (d) it never needs more space than the file system has, whereas a temp-file solution typically needs an amount of unused disk space equal to the size of the temp file (unless the temp file is written to a different file system which raises its own issues).
So this might seem an inelegant technique but it has much to recommend it - if you know the files won't be too big, for some definition of "too big".
B.O.
.
- Follow-Ups:
- Re: Modifying a file w/o creating a temp file
- From: Mike Schilling
- Re: Modifying a file w/o creating a temp file
- References:
- Modifying a file w/o creating a temp file
- From: Joe Hudson
- Re: Modifying a file w/o creating a temp file
- From: Eric Sosman
- Modifying a file w/o creating a temp file
- Prev by Date: Re: SocketChannel.write(ByteBuffer src) problem
- Next by Date: Re: error with arrayList
- Previous by thread: Re: Modifying a file w/o creating a temp file
- Next by thread: Re: Modifying a file w/o creating a temp file
- Index(es):
Relevant Pages
|