Advertisement
yo2man

Tutorial 45 Serialization: Saving Objects to Files

May 25th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. //Tutorial 45 Serialization: Saving Objects to Files
  2. //Serialization means turns objects into binary form, usually with the aim of saving them to a file. Serialization gives us an easy way to implement save and load functionality in our application. In this video we'll also finally solve the mystery of those annoying warnings you may have spotted in Eclipse about serial version UIDs, as well as answering the popular interview/exam question "how do you make an object serializable?".
  3. //if you deserialize an object, it means turning it from binary form and turning it back into an object
  4.  
  5. //Writing Objects: once you are done, righclick project>refresh> and people.bin file should appear. Just like what you did with test.txt
  6.  
  7. import java.io.Serializable;
  8.  
  9. public class Person implements Serializable { //classic interview question: how do you make a class serializable? Easy, just type "implements Serializable" then "import java.io.Serializable;"
  10.        
  11.     private int id;
  12.     private String name;
  13.    
  14.     public Person(int id, String name) {
  15.         this.id = id;
  16.         this.name = name;
  17.     }
  18.    
  19.     @Override
  20.     public String toString() {
  21.         return "Person [id=" + id + ", name=" + name + "]";
  22.     }
  23. }
  24.  
  25.  
  26. //---------------------------------------------------------------------------------------------------------------------------------------
  27. public class ReadObjects {
  28.     public static void main(String[] args){
  29.         System.out.println("Reading Objects...");
  30.        
  31.         try(FileInputStream fi = new FileInputStream("people.bin")) { //to create this: Step 1. type out>surround with try() then click on error and import and then click on error again and surround with clause
  32.            
  33.             ObjectInputStream os = new ObjectInputStream(fi); //Step 2. type out ObjectInputStream
  34.            
  35.             Person person1 = (Person)os.readObject();
  36.            
  37.             Person person2 = (Person)os.readObject();
  38.            
  39.             os.close();  
  40.            
  41.            
  42.                 System.out.println(person1);
  43.                 System.out.println(person2);
  44.            
  45.         } catch (FileNotFoundException e) { //part of Step 1.
  46.             e.printStackTrace();
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         } catch (ClassNotFoundException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53. }
  54.  
  55. //---------------------------------------------------------------------------------------------------------------------------------------
  56.  
  57.  
  58. import java.io.FileNotFoundException;
  59. import java.io.FileOutputStream;
  60. import java.io.IOException;
  61. import java.io.ObjectOutputStream;
  62.  
  63.  
  64. public class WriteObjects {
  65.  
  66.     public static void main(String[] args) {
  67.         System.out.println("Writing Objects...");
  68.        
  69.         Person mike = new Person(543, "Mike");
  70.         Person sue = new Person(123, "Sue");
  71.        
  72.         System.out.println(mike);
  73.         System.out.println(sue);
  74.        
  75.         try(FileOutputStream fs = new FileOutputStream("people.bin")) { //once you are done, righclick project>refresh> and people.bin should appear. Just like what you did with test.txt
  76.            
  77.             ObjectOutputStream os = new ObjectOutputStream(fs);
  78.            
  79.             os.writeObject(mike);  
  80.             os.writeObject(sue);
  81.            
  82.             os.close();
  83.            
  84.         } catch (FileNotFoundException e) {
  85.             e.printStackTrace();
  86.         } catch (IOException e) {
  87.             e.printStackTrace();
  88.         }
  89.        
  90.        
  91.     }
  92.  
  93. }
  94. //---------------------------------------------------------------------------------------------------------------------------------------
  95. // Run Results: If you run the Writing Tab:
  96. //a new file people.bin should be created
  97. /*
  98. Writing Objects...
  99. Person [id=543, name=Mike]
  100. Person [id=123, name=Sue]
  101. */
  102. //and for if u run the Reading tab:
  103. /*
  104. Reading Objects...
  105. Person [id=543, name=Mike]
  106. Person [id=123, name=Sue]
  107. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement