Edit a file

Discussion in 'Java' started by Rebecca, Jul 26, 2005.

  1. Rebecca

    Rebecca Guest

    I am wondering what the best way to edit a file in Java is. I have a
    file that has Header information and then footer information. in
    between is the data encircled by Strings like "START DATA" and "END
    DATA". I need a file that will contain only the info between these 2
    strings. Currently, i am reading in the data with a buffered reader
    line by line. extracting the info between thse 2 tags, and creating a
    new file with this data. Is this the best way to do something like
    this? Is there an easier/more efficient way?
    Like is the are way to open the file - remove stuff before start data,
    remove stuff after end of data and then close the file. This way i
    don't have to write a new file?

    Thanks in advance.
     
    Rebecca, Jul 26, 2005
    #1
    1. Advertising

  2. In article <>,
    "Rebecca" <> wrote:

    > I am wondering what the best way to edit a file in Java is. I have a
    > file that has Header information and then footer information. in
    > between is the data encircled by Strings like "START DATA" and "END
    > DATA". I need a file that will contain only the info between these 2
    > strings. Currently, i am reading in the data with a buffered reader
    > line by line. extracting the info between thse 2 tags, and creating a
    > new file with this data. Is this the best way to do something like
    > this? Is there an easier/more efficient way?


    This sounds like the right approach, but feel free to post code:)

    > Like is the are way to open the file - remove stuff before start data,
    > remove stuff after end of data and then close the file. This way i
    > don't have to write a new file?


    It's hard to resist the temptation to edit files "in place," but you
    must. Think of your program as a filter or function that transforms
    input into output. You want to keep the old file around until you're
    reasonably certain that you're done with it. You can always invoke
    renameTo(), delete() or deleteOnExit() when you're sure.

    > Thanks in advance.


    Been there, done that, had to dig out the backup tape:)

    --
    John
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
     
    John B. Matthews, Jul 27, 2005
    #2
    1. Advertising

  3. Rebecca wrote:
    > I am wondering what the best way to edit a file in Java is. I have a
    > file that has Header information and then footer information. in
    > between is the data encircled by Strings like "START DATA" and "END
    > DATA". I need a file that will contain only the info between these 2
    > strings. Currently, i am reading in the data with a buffered reader
    > line by line. extracting the info between thse 2 tags, and creating a
    > new file with this data. Is this the best way to do something like
    > this? Is there an easier/more efficient way?


    Script it with perl, awk, or another good scripting language. E.g. the
    following three lines of awk would do what you want:

    /^START DATA/ { f = 1; next }
    /^END DATA/ { f = 0; next }
    f == 1 { print }

    There is no need to fire up a compiler or use a language like Java for
    such text processing problems.

    > Like is the are way to open the file - remove stuff before start data,
    > remove stuff after end of data and then close the file. This way i
    > don't have to write a new file?


    You could mess around with two RandomAccessFiles in Java. But it is IMHO
    not worth the effort at all, and it is risky to perform such file
    changes in-place without some proper backup. But if you do a backup of
    the file prior to processing, you could also instead just write a new file.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Jul 27, 2005
    #3
    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. San Diego Guy
    Replies:
    0
    Views:
    560
    San Diego Guy
    Aug 7, 2003
  2. Schultz
    Replies:
    3
    Views:
    575
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. =?Utf-8?B?a2Vu?=
    Replies:
    1
    Views:
    10,358
    Wiktor Zychla [C# MVP]
    Jan 23, 2006
  4. Sharon McCarty
    Replies:
    0
    Views:
    261
    Sharon McCarty
    Nov 24, 2004
  5. Frank Kurka
    Replies:
    8
    Views:
    318
Loading...

Share This Page