1

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. 
            oos.writeObject(s);
        }
    }

read function

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

       @SuppressWarnings("unchecked") 
       ArrayList<AnimalBuild> animalList = (ArrayList<AnimalBuild>)ois.readObject(); // casting object

        Iterator it = animalList.iterator();

        while(it.hasNext()) {
         String obj = (String)it.next();
         System.out.println(obj);
    }      
}

Animal Build

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


public String Animaltype, Species, Color;
public AnimalBuild (String animaltype , String species, String color )
{
    this.Animaltype = animaltype;
    this.Species = species;
    this.Color = color;
}

public String getType() {
    return this.Animaltype;
}
public String getSpecies() {
    return this.Species;
}
public String getColor() {
    return this.Color;
}

public String setType(String newType) {
    return (this.Animaltype=newType);
}

public String setSpecies(String newSpecies) {
    return (this.Species=newSpecies);
}

public String setColor(String newColor) {
    return (this.Color=newColor);
}

public String toString ()
{
    return "\n\n Animal Type: " + this.Animaltype + "\n Species: "  + this.Species + "\n Color: " + this.Color + "\n";
}
}
3
  • you are writing each individual object to the file. if you want to write the List, then just write the List. Commented Feb 9, 2016 at 1:09
  • 1
    You can't write out a bunch of separate objects and then expect to read them back in as an ArrayList. Commented Feb 9, 2016 at 1:10
  • I'm storying all my objects in an arraylist, and I want to be able to save the arraylist full of objects to a file and than load them back in. I'm not sure Im following what you guys are saying. Commented Feb 9, 2016 at 1:14

2 Answers 2

1

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"))) {
    oos.writeObject(a);
}

To read the list

List<AnimalBuild> animalList;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("animal2.txt"))) {
     animalList = (List<AnimalBuild>) ois.readObject(); // casting object
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helped and what you said makes perfect sense.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.