Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created December 27, 2017 12:37
Show Gist options
  • Save Kishy-nivas/e7942998873b29fc936531fe548edf39 to your computer and use it in GitHub Desktop.
Save Kishy-nivas/e7942998873b29fc936531fe548edf39 to your computer and use it in GitHub Desktop.
package advancedjava;
import java.io.*;
/**
*
* @author Kishy Nivas
*/
class Student implements Serializable
{
public String name;
public int rollno;
public int age;
Student(String name ,int rollno, int age)
{
this.name = name;
this.rollno= rollno;
this.age= age;
}
}
public class SerializableExample {
public static void printData(Student s )
{
if (s== null){
System.out.println("Null data ");
return;
}
System.out.println("Name : " + s.name);
System.out.println("Roll no : " + s.rollno);
System.out.println("age: " + s.age);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO code application logic here
Student obj = new Student("kishore",23,21);
String fileName = "newfile.txt";
try
{
FileOutputStream file= new FileOutputStream(fileName);
ObjectOutputStream ob_file = new ObjectOutputStream(file);
ob_file.writeObject(obj);
}
catch(FileNotFoundException e) {
System.out.println("File not found : " + fileName);
}
System.out.println("Before serialization : " );
printData(obj);
obj = null;
printData(obj);
try
{
FileInputStream file= new FileInputStream(fileName);
ObjectInputStream ob_file = new ObjectInputStream(file);
obj= (Student) ob_file.readObject();
}
catch(FileNotFoundException e)
{
System.out.println("file not found " + fileName );
}
System.out.println("After deserialization ");
printData(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment