Hi ..
the following method is supposed to save the object in a random access file as follows ... (Object_length Object)

here's the testing code ..


public static void main(String[] args) {

     File file = new File("data.bin");
     ByteArrayOutputStream bos = null;
     ObjectOutputStream out = null;
     byte[] objectBytes = null;
     int length;

     try(RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
           Employee eMb = new Employee("John Dead", 33, 1211.2, 'M', new java.util.Date()); //The serializable object
           bos = new ByteArrayOutputStream();
           out = new ObjectOutputStream(bos);
           out.writeObject(eMb);
           objectBytes = bos.toByteArray(); //Get the byte array representing this object.
           length = objectBytes.length; //169 bytes.
           raf.writeInt(length); //Write object's length.
           raf.write(objectBytes); //Write object.
          //Current file position is 173 (length(4 bytes)  + 169 bytes);
      
          bos.reset();
           out.reset();
       
          //Add Another One.
           eMb = new Employee("John Deads", 34, 8999.0, 'M', new java.util.Date());
           out.writeObject(eMb);
           objectBytes = bos.toByteArray();
           length = objectBytes.length; //170 bytes.
           raf.writeInt(length); //Write object's length.
           raf.write(objectBytes); //Write object.
          //Current file position is 347 (length(4 bytes)  + 170 bytes + 173 bytes);

          //---- Test Reading
          //--- First i read the object's length and allocate a byte array to hold it
          //---- Reading first object
           raf.seek(0);
           length = raf.readInt(); //length is 169 when printed.
           objectBytes = new byte[length];
           raf.readFully(objectBytes);
           eMb = (Employee)readObjectFromByteArray(objectBytes); //This method is defined below
           eMb.printInfo();
          //Current file position is 173
       
          //---- Reading second object (fail)
           length = raf.readInt(); //length is 170 when printed.
           objectBytes = new byte[length];
           raf.readFully(objectBytes);
           //Current file position is 347
           eMb = (Employee)readObjectFromByteArray(objectBytes); //Exception thrown.
           eMb.printInfo();
     }

     //Some catch() blocks ...
}

public static Object readObjectFromByteArray(byte[] objectBytes) throws IOException, ClassNotFoundException { //i.e. deserialize
          ByteArrayInputStream bis = new ByteArrayInputStream(objectBytes);
          ObjectInputStream in = new ObjectInputStream(bis); //Exception is thrown on reading the second object.
          return in.readObject();
}



 

StreamCorruptedException is thrown on trying to read the second object, am i misusing something here or doing something wrong? sorry my programming background wasn't Java
here's the stack trace:

 

java.io.StreamCorruptedException: invalid stream header: 79737200
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at testpack.FileRandomAccess.readObjectFromByteArray(FileRandomAccess.java:57)



    at testpack.FileRandomAccess.main(FileRandomAccess.java:48)
FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners