1

I have a List of list of some objects defined in a data structure: List > someObject1=new ArrayList();

And I have written few someObject items to a fileXY using function writeData :

void writeData(List <List<Integer>> someObject1, File XY) throws IOException {
    try(ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(XY,true)))
    outFile.writeObject(someObject1);
    }
    catch( IOException ex) { ex.printStackTrace(); } 
}

Now I tried to read it using function readData:

void readData(File XY) throws IOException {
    try (ObjectInputStream inFile=new ObjectInputStream(new FileInputStream(XY))) {
    List <List <MyClass>> someObject2=(List <List <MyClass>>)inFile.readObject();
    }
catch( IOException ex) { ex.printStackTrace(); } 
}

But it is giving me error :

java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1363)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)

Can anyone help me with it. Is there something wrong with the casting while reading this data structure?

Had it been just a list of objects, I could have read it by casting the readObject() with

(List<MyClass>).

Why the same thing doesn't work for the list-of-list of objects?

This is outline of MyClass:

public class MyClass implements Serializable {
private long aa;    
private List <Integer> bb;
public MyClass(long a,List <Integer> b) {
    aa=a;
    bb=b;         
}
public long getA() {    
    return aa;
}         

public List<Integer> getB () {
   return bb; 
} 

}

8
  • 1
    Can you include the code where you close the outFile ? Commented Apr 16, 2014 at 7:53
  • 1
    Can you also include the relevant parts of 'MyClass'? Does it have custom readObject()/writeObject() methods? If so, show them. Commented Apr 16, 2014 at 7:59
  • 1
    Don't forget to close the ObjectOutputStream and ObjectInputStream after you write/read to it! Commented Apr 16, 2014 at 8:03
  • Also your code doesn't actually compile. If you're trying to append to an object stream file, you can't, at least not without special treatment, otherwise you will write a duplicate stream header in the middle of the file. But that normally causes a StreamCorruptedException with 'invalid type code: AC'. And not closing the file usually causes an EOFException. So my money is on something strange in custom serialization methods. It can't be the casting, as you haven't got to that yet. Commented Apr 16, 2014 at 8:05
  • 1
    @Kaur the try will implicitely close it only if it's a try-with-resource like this: try(stream declaration) { code } Commented Apr 16, 2014 at 8:31

1 Answer 1

1

This definitely works for me:

public static final String FILE = "file.b";

public static class MyClass implements Serializable {
    private long aa;
    private List<Integer> bb;

    public MyClass(long a, List<Integer> b) {
        aa = a;
        bb = b;
    }
}

public static void main(String[] args) throws FileNotFoundException, IOException,
        ClassNotFoundException {
    List<List<MyClass>> list = new ArrayList<>();
    list.add(new ArrayList<>());
    list.get(0).add(new MyClass(5, new ArrayList<>()));

    ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(new File(FILE)));
    outFile.writeObject(list);
    outFile.close();

    ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(new File(FILE)));
    List<List<MyClass>> list2 = (List<List<MyClass>>) inFile.readObject();
    System.out.println(list2);
}

Note: this works even if I don't add any MyClass to list.get(0), and even if I don't add a list to list.

Several possible causes for your problem come to my mind:

  • you forgot to close the output stream after writing to it
  • you don't read the object at the right position in the file. If you have several objects written to the file, they must be read in the same order as they were written.
  • your problem is with your custom class

UPDATE:

Maybe you use a non serializable attribute in your serializable class MyClass. If so, you should override writeObject() and readObject() to serialize/deserialize such an attribute of your class properly.

Take a look at this: http://marxsoftware.blogspot.fr/2014/02/serializing-java-objects-with-non.html

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Joffrey. I haven't overrided the writeObject and readObject method. I have used only Serializable Interface in MyClass.
@Kaur I understand you don't override those methods as of right now, but if you have non-serializable attributes, you should do so if you want it to work. Are there any other types of attributes in your custom class MyClass?
No, I have a simple class with these two attributes only - as mentioned in cide above. Do I still need to implement/override readObject()/writeObject() methods?
@Kaur I don't think so, because List is serializable. I updated the code that works for me with your class. Still works. You problem must be somewhere else.
Thanks for your efforts. I will have a look again at my code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.