Question

Please could you please amend the java code below to serialize and deserilize an object using...

Please could you please amend the java code below to serialize and deserilize an object using FileOutputStrean and FileInputStream.

Please could I have a typewritten answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java Code to Serialize and Deserialize the objects

import java.io.*;
import java.io.Serializable;
class Dog implements Serializable {
String breed;
int age;
String colour;

//Constructor of class Dog
public Dog(String breed, int age, String colour) {
this.breed = breed;
this.age = age;
this.colour=colour;
}
}

public class SerDeser{
public static void main(String args[])throws Exception{

//Creating object using constructor
Dog d =new Dog("BullDog", 2,"Brown");
FileOutputStream fout=new FileOutputStream("file1.txt");
ObjectOutputStream out=new ObjectOutputStream(fout); //converting objects into a stream
out.writeObject(d); //Writing object to a file
out.flush();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("file1.txt")); //Reading Streams into objects
Dog d1=(Dog)in.readObject(); //Reading object from a file
System.out.println(d1.breed+" "+d1.age+" "+d1.colour); //Printing the object
in.close();
}
}

$javac SerDeser.java

$java SerDeser

BullDog 2 Brown

Add a comment
Know the answer?
Add Answer to:
Please could you please amend the java code below to serialize and deserilize an object using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT