The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


DeSerializing without class casting.

Posted by M Nelson on January 16, 2001 at 3:42 PM

> I've Serialized object in database in String format, I want to know how to bring it back to
> object without ClassCasting , because the program that do unserialize is
> know classname at Runtime so it can't use ClassCasting Method

If you have searalized the object to a file, you should be able to
read the object back from the searalized file without having to
cast it.

User the readObject method of an ObjectInputStream to obtain an
instance of Object. Then you can use reflection as necessary
to determine the class of the object you have just read.

Something like:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("serialfile.ser")):
Object o = ois.readObject();
System.out.prinln("Object Class is: " + o.getClass().getName());

For more information on serialization, see the very helpful
TechTip on Sun's web site:


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us