Skip to content

Instantly share code, notes, and snippets.

@skayred
Created October 16, 2011 15:40
Show Gist options
  • Save skayred/1291051 to your computer and use it in GitHub Desktop.
Save skayred/1291051 to your computer and use it in GitHub Desktop.
Serialization-deserialization example
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args) {
Test test = new Test(123, 456);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(test);
byte[] message = baos.toByteArray();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message));
Test newTest = (Test) in.readObject();
System.out.println(newTest.getFirst());
System.out.println(newTest.getSecond());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment