Re: Modifying a file w/o creating a temp file
- From: "Mike Schilling" <mscottschilling@xxxxxxxxxxx>
- Date: Fri, 23 May 2008 18:26:04 -0700
Bernie Ohls wrote:
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".
Of course, if your program crashes or is killed in the middle, you've
lost the file entirely. One big advantage of the temp file method is
that the rename is atomic, so whatever happens the result is either
the original version of the file or the rewritten one.
.
- Follow-Ups:
- Re: Modifying a file w/o creating a temp file
- From: Joe Hudson
- 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
- Re: Modifying a file w/o creating a temp file
- From: Bernie Ohls
- Modifying a file w/o creating a temp file
- Prev by Date: Re: Optimisation for animated MultipleGradientPaint code
- Next by Date: Re: SocketChannel.write(ByteBuffer src) problem
- 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
|
|