Deserializing an Object

This example deserializes a javax.swing.JButton object.

See also Serializing an Object.

try { // Deserialize from a file File file = new File("filename.ser"); ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); // Deserialize the object javax.swing.JButton button = (javax.swing.JButton) in.readObject(); in.close(); // Get some byte array data byte[] bytes = getBytesFromFile(file); // see Reading a File into a Byte Array for the implementation of this method // Deserialize from a byte array in = new ObjectInputStream(new ByteArrayInputStream(bytes)); button = (javax.swing.JButton) in.readObject(); in.close(); } catch (ClassNotFoundException e) { } catch (IOException e) { }

Comments

20 Jan 2010 - 7:37am by Anonymous (not verified)

Nice and clean information. Like it for its simplicity

24 Jan 2010 - 6:55am by SP (not verified)

in deserializing from a byte array over network, in does not recognize "bytes" in the line
in = new ObjectInputStream(new ByteArrayInputStream(bytes));
plz help. i'm new to Serializing concept and networking in java

30 Apr 2010 - 12:47am by Srinivas Pinisetti (not verified)

bytes is the instance of byte[] (byte[] bytes = getBytesFromFile(file); )

You need to use something like URLConnection / sockets to read bytes from network resource.

11 May 2010 - 1:23am by Anonymous (not verified)

Does anyone know of a problem with serialization with the newest Java version? Some code I had used for a year suddenly won't deserialize and returns an exception.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.