Advertisement
yo2man

Tutorial 46 Serializing Arrays

May 26th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. //Tutorial 46 Serializing Arrays
  2. //no idea whats going on
  3.  
  4. //---------------------------------------------------------------------------------------------------------------------------------------
  5. //Person.java
  6. import java.io.Serializable;
  7.  
  8. public class Person implements Serializable {
  9.     /**
  10.      *
  11.      */
  12.     private static final long serialVersionUID = 4801633306273802062L;
  13.    
  14.     private int id;
  15.     private String name;
  16.    
  17.     public Person(int id, String name) {
  18.         this.id = id;
  19.         this.name = name;
  20.     }
  21.  
  22.     @Override
  23.     public String toString() {
  24.         return "Person [id=" + id + ", name=" + name + "]";
  25.     }
  26. //---------------------------------------------------------------------------------------------------------------------------------------
  27. //WriteObjects.java
  28. import java.io.FileNotFoundException;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.io.ObjectOutputStream;
  32. import java.util.ArrayList;
  33. import java.util.Arrays;
  34.  
  35.  
  36. public class WriteObjects {
  37.  
  38.     public static void main(String[] args) {
  39.         System.out.println("Writing objects...");
  40.        
  41.         Person[] people = {new Person(1, "Sue"), new Person(99, "Mike"), new Person(7, "Bob")};
  42.  
  43.         ArrayList<Person> peopleList = new ArrayList<Person>(Arrays.asList(people));
  44.        
  45.         try(FileOutputStream fs = new FileOutputStream("test.ser")) {
  46.        
  47.             ObjectOutputStream os = new ObjectOutputStream(fs);
  48.            
  49.             // Write entire array
  50.             os.writeObject(people);
  51.            
  52.             // Write arraylist
  53.             os.writeObject(peopleList);
  54.            
  55.             // Write objects one by one
  56.             os.writeInt(peopleList.size());
  57.            
  58.                 for(Person person: peopleList){
  59.                     os.writeObject(person);
  60.                 }
  61.                                    
  62.            
  63.             os.close();
  64.            
  65.         } catch (FileNotFoundException e) {
  66.             e.printStackTrace();
  67.         } catch (IOException e) {
  68.             e.printStackTrace();
  69.         }
  70.         }
  71.  
  72. }
  73.  
  74. //---------------------------------------------------------------------------------------------------------------------------------------
  75. //ReadObjects.java
  76.  
  77. import java.io.FileInputStream;
  78. import java.io.FileNotFoundException;
  79. import java.io.IOException;
  80. import java.io.ObjectInputStream;
  81. import java.util.ArrayList;
  82.  
  83.  
  84. public class ReadObjects {
  85.     public static void main(String[] args){
  86.         System.out.println("Reading Objects...");
  87.        
  88.        
  89.         try(FileInputStream fi = new FileInputStream("test.ser")) {
  90.            
  91.             ObjectInputStream os = new ObjectInputStream(fi);
  92.            
  93.             // Read entire array
  94.             Person[] people = (Person[])os.readObject();
  95.            
  96.             // Read entire arraylist
  97.             @SuppressWarnings("unchecked") //Suppresses warnings
  98.             ArrayList<Person> peopleList = (ArrayList<Person>)os.readObject();
  99.            
  100.             for(Person person: people) {
  101.                 System.out.println(person);
  102.             }
  103.             for(Person person: peopleList) {
  104.                 System.out.println(person);
  105.             }
  106.            
  107.             // Read objects one by one.
  108.             int num = os.readInt();
  109.            
  110.             for(int i=0; i<num; i++) {
  111.                 Person person = (Person)os.readObject();
  112.                 System.out.println(person);
  113.             }
  114.            
  115.             os.close();  
  116.            
  117.         } catch (FileNotFoundException e) {
  118.             e.printStackTrace();
  119.         } catch (IOException e) {
  120.             e.printStackTrace();
  121.         } catch (ClassNotFoundException e) {
  122.             e.printStackTrace();
  123.            
  124.         }
  125.     }
  126. }
  127.  
  128. //---------------------------------------------------------------------------------------------------------------------------------------
  129. //if you run the ReadObjects.java tab this should be the run result:
  130. /*
  131. Reading Objects...
  132. Person [id=1, name=Sue]
  133. Person [id=99, name=Mike]
  134. Person [id=7, name=Bob]
  135. Person [id=1, name=Sue]
  136. Person [id=99, name=Mike]
  137. Person [id=7, name=Bob]
  138. Person [id=1, name=Sue]
  139. Person [id=99, name=Mike]
  140. Person [id=7, name=Bob]
  141. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement