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) {
}
Nice and clean information. Like it for its simplicity
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
bytes is the instance of byte[] (byte[] bytes = getBytesFromFile(file); )
You need to use something like URLConnection / sockets to read bytes from network resource.
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.