Results 1 to 3 of 3
  1. #1
    spmchugh82 is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default How to open a file without erasing its contents?

    I think what happening here is FileOutputStream is erasing the file and so when input.readObject() is called it's reading from a blank file. Is that right? Is there any way I can write to the file without recreating it? Maybe FileWriter?


    input = new ObjectInputStream(new FileInputStream(dataFile));
    output = new ObjectOutputStream(new FileOutputStream(dataFile));
    table = (Table) input.readObject();

  2. #2
    spmchugh82 is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Okay, I just got it to work by switching around output = and table=

    input = new ObjectInputStream(new FileInputStream(dataFile));
    table = (Table) input.readObject();
    output = new ObjectOutputStream(new FileOutputStream(dataFile));

    ...so this works fine. But is there a better way to do it?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Have a look at the FileOutputStream API especially the constructors. There's one available that uses a boolean that will allow you to append to a file rather than overwrite the file.

Similar Threads

  1. Writing to excel file erasing existing formatting
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 06:58 PM
  2. seperation of file contents
    By Ms.Ranjan in forum New To Java
    Replies: 5
    Last Post: 07-11-2008, 10:17 PM
  3. Replies: 9
    Last Post: 07-10-2008, 05:52 PM
  4. Viewing contents of zip file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 06:16 PM
  5. Viewing contents of JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •