Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Modifying a file w/o creating a temp file

Reply
Thread Tools

Modifying a file w/o creating a temp file

 
 
Joe Hudson
Guest
Posts: n/a
 
      05-23-2008
Hi,

I have a bit of text I want to replace in a file with some other
text. I am aware of how to do this by creating a temp file and
renaming the temp file to the original file name after processing.
However, I am trying to find a way to modify content of a file. Is
there any way I can do something like this file.replace(srcIndexStart,
srcIndexEnd, "replace text");

I know the method I gave is not on the java.io.class and, for that
matter, I would at least use an OutputStream to do this normally.
But, I am beginning to think that what I am looking for isn't an
option with Java. I very much appreciate any insight that someone
might be willing to offer. Thanks.

Joe
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      05-24-2008
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.


 
Reply With Quote
 
 
 
 
Joe Hudson
Guest
Posts: n/a
 
      05-24-2008
Thank you all very much for your help on this.

Joe
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      05-24-2008
Joe Hudson wrote:
> I have a bit of text I want to replace in a file with some other
> text. I am aware of how to do this by creating a temp file and
> renaming the temp file to the original file name after processing.
> However, I am trying to find a way to modify content of a file. Is
> there any way I can do something like this file.replace(srcIndexStart,
> srcIndexEnd, "replace text");
>
> I know the method I gave is not on the java.io.class and, for that
> matter, I would at least use an OutputStream to do this normally.
> But, I am beginning to think that what I am looking for isn't an
> option with Java. I very much appreciate any insight that someone
> might be willing to offer.


As other already have stated, then you can not do that
unless the replacement has the exact same length in bytes.

If not then you will at least need to rewrite from the start
of the changed location.

And I would say that:

small text => rewrite the entire file

large text => use something that is not a sequential file
(structured files with linked lines / index sequential file with
a record per line / database table with a row per line)

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.36 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? PerlFAQ Server Perl Misc 0 04-26-2011 10:00 AM
Editing a file without creating a temp file Sooraj S C++ 2 01-19-2011 01:48 PM
How to initialize member reference with temp object and delete the temp object when the destructor is called? PengYu.UT@gmail.com C++ 2 05-09-2005 05:48 PM
File::Temp: opening the temp. file in "r+" mode? Also "man in the middle" A. Farber Perl Misc 3 03-03-2004 12:42 PM
Temp Internet and temp files now in winnt directory w2k pro Bibble Bobble Computer Support 1 11-27-2003 06:14 PM



Advertisments