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

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.