Home Ask Login Register

Developers Planet

Your answer is one click away!

user3369825 February 2016

Java can't get object to cast from file to arraylist

I'm trying to load an object in from a file. I first create the file by saving an object to it. If I save only one object to a file I can get it loaded in by casting the object into a variable stead of an arraylist. But if I try to cast mulitple objects into an arraylist I keep getting errors. I sometimes will get this:

animalkingdom.AnimalBuild; local class incompatible: stream classdesc serialVersionUID = 8814442576780984798, local class serialVersionUID = -7073710162342893881

or this

Exception in thread "main" java.lang.ClassCastException: animalkingdom.AnimalBuild cannot be cast to java.util.ArrayList at animalkingdom.AnimalKingdom.readFile(AnimalKingdom.java:146) at animalkingdom.AnimalKingdom.main(AnimalKingdom.java:123) Java Result: 1

write function

  // function to write object to file
       public static void writeToFile(ArrayList<AnimalBuild> a) throws    IOException  { 
        ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream("animal2.txt"));

        for (AnimalBuild s : a) { // loop through and write objects to file. 

read function

  // function to read from file
     public static void readFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("animal2.txt"));

       ArrayList<AnimalBuild> animalList = (ArrayList<AnimalBuild>)ois.readObject(); // casting object

        Iterator it = animalList.iterator();

        while(it.hasNext()) {
         String obj = (String)it.next();

Animal Build

class AnimalBuild implements Serializable {
private static final long serialVersionUID = 8814442576780984798L;
//private static fina        


Peter Lawrey February 2016

When you serialize data you need to read it in a compatible manner to how it was written. You are writing each element individually so to read this you would need to read them individually.

However, writing a list is simpler.

try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("animal2.txt"))) {

To read the list

List<AnimalBuild> animalList;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("animal2.txt"))) {
     animalList = (List<AnimalBuild>) ois.readObject(); // casting object

T.S February 2016

Read the object one by one then add to list. Just reverse the way you write the object. Check sample code below:

List<AnimalBuild> animalList  = new ArrayList<AnimalBuild>();
Object obj = null;
while ((obj = ois.readObject()) != null) {
    if (obj instanceof AnimalBuild) {
    AnimalBuild ab = (AnimalBuild) obj;
    animalList .add(ab);

You could write the entire list as one object to the file and then read it back as same object rather than writing one by one.

Post Status

Asked in February 2016
Viewed 2,922 times
Voted 9
Answered 2 times


Leave an answer

Quote of the day: live life