How do you write to text files WITHOUT OVERWRITING?

Discussion in 'Java' started by javajavalink, Dec 14, 2004.

  1. javajavalink

    javajavalink Guest

    I am relatively new to java and have already tried many ways to write
    to text files with Java. Is there any way to write to a text file
    without overwriting what is already there??
    javajavalink, Dec 14, 2004
    #1
    1. Advertising

  2. javajavalink

    Rob Guest

    javajavalink wrote:
    > I am relatively new to java and have already tried many ways to write
    > to text files with Java. Is there any way to write to a text file
    > without overwriting what is already there??
    >

    -You could append to the file, using something like:
    BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));

    -You could look up RandomFileAccess
    -You could read the file into a String, manipulate the string
    and write it back out.
    Rob, Dec 14, 2004
    #2
    1. Advertising

  3. javajavalink

    Guest

    If you create a new instance of a FileReader with just the filename,
    then that file will be erased if it is already there. You can use the
    constructor of FileReader that accepts the filename and a boolean. If
    the boolean is true, then the file will be appended.
    , Dec 14, 2004
    #3
  4. javajavalink

    Guest

    If you create a new instance of a FileReader with just the filename,
    then that file will be erased if it is already there. You can use the
    constructor of FileReader that accepts the filename and a boolean. If
    the boolean is true, then the file will be appended.
    , Dec 14, 2004
    #4
  5. > I am relatively new to java and have already tried many ways to write
    > to text files with Java. Is there any way to write to a text file
    > without overwriting what is already there??


    None of the most used filesystems can insert data at a random place without
    overwriting what is already there. So all operating systems and most
    programming languages work in the same way. Databases use tree structures to
    make it possible to insert data in the middle without overwriting anything.

    Niels Dybdahl
    Niels Dybdahl, Dec 14, 2004
    #5
  6. javajavalink

    Storm Guest

    Niels Dybdahl wrote:
    > > I am relatively new to java and have already tried many ways to

    write
    > > to text files with Java. Is there any way to write to a text file
    > > without overwriting what is already there??

    >
    > None of the most used filesystems can insert data at a random place

    without
    > overwriting what is already there. So all operating systems and most
    > programming languages work in the same way. Databases use tree

    structures to
    > make it possible to insert data in the middle without overwriting

    anything.
    >
    > Niels Dybdahl


    Actually yes you can insert data at random places by utilizing the
    RandomFile class. By supplying the length of each record, and the end
    record number, you can reset the file size to the oldsize + newRec
    length and boom you have an append. Also, you can write data into the
    middle of a file without overwriting. All you have to do is create a
    class that represents each row, read each record into the created
    class, insert the new record at the appropriate point in the vector
    during a For/While loop, and re-write the file using the data present
    in the vector.

    Storm
    Storm, Dec 14, 2004
    #6
  7. Storm wrote:
    > Actually yes you can insert data at random places by utilizing the
    > RandomFile class.


    What "RandomFile" class? Do you mean java.io.RandomAccessFile? Then
    you're wrong.

    > By supplying the length of each record, and the end
    > record number, you can reset the file size to the oldsize + newRec
    > length and boom you have an append.


    Nobody said anything about appending. That can be done.

    > Also, you can write data into the
    > middle of a file without overwriting.


    No, you cannot.

    > All you have to do is create a
    > class that represents each row, read each record into the created
    > class, insert the new record at the appropriate point in the vector
    > during a For/While loop, and re-write the file using the data present
    > in the vector.


    Please tell us that was a joke.
    Michael Borgwardt, Dec 14, 2004
    #7
    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. Stewart
    Replies:
    8
    Views:
    791
    Stewart
    Nov 5, 2004
  2. dev648237923

    Deploy without overwriting Web.Config

    dev648237923, Jan 17, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    522
    Walter Wang [MSFT]
    Jan 23, 2007
  3. Steven D'Aprano

    Safely renaming a file without overwriting

    Steven D'Aprano, Oct 28, 2006, in forum: Python
    Replies:
    11
    Views:
    461
    Wolfgang Draxinger
    Oct 29, 2006
  4. Cowboy \(Gregory A. Beamer\)

    SOLUTION: Deploying web apps without overwriting config values

    Cowboy \(Gregory A. Beamer\), Oct 4, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    340
    Cowboy \(Gregory A. Beamer\)
    Oct 4, 2008
  5. Reckoner
    Replies:
    6
    Views:
    275
    Fernando H. Sanches
    Oct 25, 2008
Loading...

Share This Page