Search code examples
javaobjectappendbinaryfiles

Appendable Output Stream - Java


I'm working on appending objects to a binary file. My professor has provided an "appendable" output stream class for us to use on this assignment, and from my understanding this is what should prevent a corrupted header. However, I'm still getting a corrupted header when I attempt to open the binary file. The name of the file is test.dat and as far as I can tell the program writes the data just fine, but as soon as I try reading from it everything goes out the window.

fileName is a data field in the same class these methods are defined in and is defined as follows File filename = new File("test.dat");

If anyone could point me in the right direction that would be fantastic! Thanks in advance

My Code

 /**
 Writes a pet record to the file

 @param pets The pet record to write
 */
 public static void writePets(PetRecord pet){
   AppendObjectOutputStream handle = null;
   try{
     handle = new AppendObjectOutputStream(new FileOutputStream(fileName, true));
     handle.writeObject(pet);
     handle.flush();
   } catch (IOException e){
    System.out.println("Fatal Error!");
    System.exit(0);
   } finally {
   try{
    handle.close();
   } catch (IOException e){
     e.printStackTrace();
   }
 }
}

  /**
  Reads all pets from the file so long as the user continues to enter "next"
  */
  public static void readPets(){
    Scanner keys = new Scanner(System.in);
    String input = "";
    ObjectInputStream handle = null;
    PetRecord pet = null;
    try{
      handle = new ObjectInputStream(new FileInputStream(fileName)); // stack trace points here
      do{
        try{
          pet = (PetRecord) handle.readObject();
          System.out.println("\n" + pet);
          System.out.println("[*] type \"next\" to continue");
          input = keys.nextLine();
        } catch (IOException e){
          System.out.println("\t[*] No More Entries [*]");
          e.printStackTrace();
          break;
        }
      } while (input.matches("^n|^next"));
      handle.close();
    } catch (ClassNotFoundException e){
      System.out.println("The dat file is currupted!");
    } catch (IOException e){
      System.out.println("\t[*] No Entries! [*]");
      e.printStackTrace();
    }
  }

Provided class:

public class AppendObjectOutputStream extends ObjectOutputStream
{
   // constructor
   public AppendObjectOutputStream( OutputStream out ) throws IOException
   {
      // this constructor just calls the super (parent)
      super(out);
   }

   @Override
   protected void writeStreamHeader() throws IOException
   {
      // this forces Java to clear the previous header, re-write a new header,
      // and prevents file corruption
      reset();
   }
}

Stack Track:

java.io.StreamCorruptedException: invalid stream header: 79737200
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:808)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
    at UIHandle.readPets(UIHandle.java:381)
    at UIHandle.list(UIHandle.java:79)
    at UIHandle.command(UIHandle.java:103)
    at UIHandle.mainUI(UIHandle.java:40)
    at UIHandle.main(UIHandle.java:405)

Solution

  • Turns out it helps if you make sure a file exits before appending to it. The problem wasn't with reading the file, but attempting to append to a file when it wasn't there. The fix was a simple if/else to check to see if the file existed. If it doesn't exist then write the file as usual, if it does exist then use the custom append class.

      /**
      Writes a pet record to the file
      @param pet The pet record to write
      */
      public static void writePet(PetRecord pet){
        if (fileName.exists()){
          AppendObjectOutputStream handle = null;
          try{
            handle = new AppendObjectOutputStream(new FileOutputStream(fileName, true));
            handle.writeObject(pet);
            handle.flush();
          } catch (IOException e){
            System.out.println("Fatal Error!");
            System.exit(0);
          } finally {
            try{
              handle.close();
            } catch (IOException e){
              e.printStackTrace();
            }
          }
        } else {
          ObjectOutputStream handle = null;
          try{
            handle = new ObjectOutputStream(new FileOutputStream(fileName));
            handle.writeObject(pet);
            handle.flush();
          } catch (IOException e){
            System.out.println("Fatal Error!");
            System.exit(0);
          } finally {
            try{
              handle.close();
            } catch (IOException e){
              e.printStackTrace();
            }
          }
        }
      }