Created
May 1, 2013 12:13
-
-
Save anonymous/5494971 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.*; | |
| class Test implements Serializable { | |
| transient int i=10; | |
| int j=5; | |
| transient boolean foo = true; | |
| boolean bar = true; | |
| } | |
| public class Serial { | |
| public static void main(String [] args) { | |
| Test test = new Test(); | |
| try { | |
| FileOutputStream fileOut = | |
| new FileOutputStream("Test.ser"); | |
| ObjectOutputStream out = new ObjectOutputStream(fileOut); | |
| out.writeObject(test); | |
| out.close(); | |
| fileOut.close(); | |
| } catch(IOException i) { | |
| i.printStackTrace(); | |
| } | |
| Test loaded = null; | |
| try { | |
| FileInputStream fileIn = new FileInputStream("Test.ser"); | |
| ObjectInputStream in = new ObjectInputStream(fileIn); | |
| loaded = (Test) in.readObject(); | |
| in.close(); | |
| fileIn.close(); | |
| }catch(IOException i) { | |
| i.printStackTrace(); | |
| return; | |
| }catch(ClassNotFoundException c) { | |
| System.out.println("Test class not found"); | |
| c.printStackTrace(); | |
| return; | |
| } | |
| System.out.println("Deserialized Test..."); | |
| System.out.println("i: " + loaded.i); | |
| System.out.println("j: " + loaded.j); | |
| System.out.println("foo: " + loaded.foo); | |
| System.out.println("bar: " + loaded.bar); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment