Created
October 16, 2011 15:40
-
-
Save skayred/1291051 to your computer and use it in GitHub Desktop.
Serialization-deserialization example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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