简体   繁体   中英

Reading object from file using ObjectInputStream? ClassNotFoundException

In previous code in my program, I had saved an ArrayList (consisting of objects of a custom class called location as you can see in my code) in a file using ObjectOutputStream and FileOutPutStream. However, when trying to retrieve the object from the file, using ObjectInputStream, I am getting an error saying that I have an unhandled exception (ClassNotFoundException).

Here's the code I used to get the ArrayList out of the file:

String file = "file";

ObjectInputStream input = new ObjectInputStream(new FileInputStream("file"));



ArrayList<location> arrayList = new ArrayList<location>();
arrayList = (ArrayList) input.readObject();

The error is on the line where I call the .readObject() method. Any Help will be appreciated as I am new to Java. Thank You!

That means the class you sent could not be found in your app. You have to add it to the class path of the app, or only send classes the app has. In your case, the missing class will be in the ArrayList as ArrayList will always be there.

Nothing mysterious is going on, the error means just what it says.

It would be more useful if the exception told you which class was missing. I think Java 7 does this now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM