Created
September 11, 2013 08:14
-
-
Save tai2/6520673 to your computer and use it in GitHub Desktop.
Java Object serialization/deserialization
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.*; | |
| import java.util.*; | |
| public class SerializeTest | |
| { | |
| private static void serialize() | |
| { | |
| Map<String, List<String>> headers = new HashMap<String, List<String>>(); | |
| List<String> val1 = new ArrayList<String>(); | |
| val1.add("val1"); | |
| headers.put("key1", val1); | |
| List<String> val2 = new ArrayList<String>(); | |
| val2.add("val2"); | |
| headers.put("key2", val2); | |
| List<String> val3 = new ArrayList<String>(); | |
| val3.add("val3"); | |
| headers.put("key3", val3); | |
| try { | |
| FileOutputStream fout = new FileOutputStream("test.headers"); | |
| ObjectOutputStream oout = new ObjectOutputStream(fout); | |
| oout.writeObject(headers); | |
| oout.close(); | |
| fout.close(); | |
| } catch (IOException e) { | |
| System.err.println(e.getMessage()); | |
| } | |
| } | |
| private static void deserialize() | |
| { | |
| Map<String, List<String>> headers = null; | |
| try { | |
| FileInputStream fin = new FileInputStream("test.headers"); | |
| ObjectInputStream oin = new ObjectInputStream(fin); | |
| headers = (Map<String, List<String>>)oin.readObject(); | |
| oin.close(); | |
| fin.close(); | |
| } catch (IOException e) { | |
| System.err.println(e.getMessage()); | |
| } catch (ClassNotFoundException e) { | |
| System.err.println(e.getMessage()); | |
| } | |
| for (String key : headers.keySet()) { | |
| List<String> val = headers.get(key); | |
| System.out.println(key + ":" + val.get(0)); | |
| } | |
| } | |
| public static void main(String[] args) | |
| { | |
| serialize(); | |
| deserialize(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment