Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dog implements Serializable
- {
- private String name;
- private String color;
- // Constructor (used to make the object)
- public Dog(String name, String color)
- {
- this.name = name;
- this.color = color;
- }
- public String getName(){
- return name;
- }
- public String getColor(){
- return color;
- }
- }
- class Test
- {
- public static void main(String[] args)
- {
- Object object1 = null;
- try
- {
- // Reading the object from a file
- FileInputStream file = new FileInputStream(filename);
- ObjectInputStream in = new ObjectInputStream(file);
- // Method for deserialization of object
- // We have to cast the object read from the byte stream to a Dog
- object1 = (Dog)in.readObject();
- in.close();
- file.close();
- System.out.println("Object has been deserialized ");
- System.out.println("name = " + object1.getName());
- System.out.println("color = " + object1.getColor());
- }
- catch(IOException ex)
- {
- System.out.println("IOException is caught");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement