Modifying a file w/o creating a temp file

Discussion in 'Java' started by Joe Hudson, May 23, 2008.

  1. Joe Hudson

    Joe Hudson Guest

    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
     
    Joe Hudson, May 23, 2008
    #1
    1. Advertising

  2. 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.
     
    Mike Schilling, May 24, 2008
    #2
    1. Advertising

  3. Joe Hudson

    Joe Hudson Guest

    Thank you all very much for your help on this.

    Joe
     
    Joe Hudson, May 24, 2008
    #3
  4. Joe Hudson

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, May 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    594
    Mark P
    May 9, 2005
  2. Sooraj S
    Replies:
    2
    Views:
    409
    Dietmar Kuehl
    Jan 19, 2011
  3. Colin Mackay

    IIS having problems creating temp directories

    Colin Mackay, Aug 11, 2003, in forum: ASP .Net Security
    Replies:
    1
    Views:
    197
    Colin Mackay
    Aug 11, 2003
  4. A. Farber
    Replies:
    3
    Views:
    299
    Ben Morrow
    Mar 3, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    280
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page