Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. class Dog implements Serializable
  2. {
  3.     private String name;
  4.     private String color;
  5.      
  6.     // Constructor (used to make the object)
  7.     public Dog(String name, String color)
  8.     {
  9.         this.name = name;
  10.         this.color = color;
  11.     }
  12.  
  13. public String getName(){
  14. return name;
  15. }
  16.  
  17. public String getColor(){
  18. return color;
  19. }
  20.  
  21. }
  22. class Test
  23. {
  24.     public static void main(String[] args)
  25.     {    
  26.         Object object1 = null;
  27.        try
  28.         {    
  29.             // Reading the object from a file
  30.             FileInputStream file = new FileInputStream(filename);
  31.             ObjectInputStream in = new ObjectInputStream(file);
  32.              
  33.             // Method for deserialization of object
  34.             // We have to cast the object read from the byte stream to a Dog
  35.             object1 = (Dog)in.readObject();
  36.              
  37.             in.close();
  38.             file.close();
  39.              
  40.             System.out.println("Object has been deserialized ");
  41.             System.out.println("name = " + object1.getName());
  42.             System.out.println("color = " + object1.getColor());
  43.         }
  44.          
  45.         catch(IOException ex)
  46.         {
  47.             System.out.println("IOException is caught");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement