import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class SerializeData {
	public static void main(String[] args)	{
		//just for determining the current year
		GregorianCalendar gregCal = new GregorianCalendar();

		Person hans = new Person();
		hans.name = new String("hans");
		hans.yearOfBirth = 1950;
		hans.age = gregCal.get(Calendar.YEAR) - hans.yearOfBirth;

		System.out.println("object before serialization:\n" + hans.toString() );

		ObjectOutputStream oos = null;

		try {
			oos = new ObjectOutputStream( new FileOutputStream("hans") );
			oos.writeObject ( hans );
		} catch (IOException ioe) {
			System.out.println("an IOException occurred");
			System.out.println( ioe.getMessage() );
		} finally {
			try {
				oos.close();
			} catch (IOException ioe) {
				//ignore it
			} //catch
		} //finally

		gregCal = null;
		hans = null;

		ObjectInputStream ois = null;
		try {
			Person anotherOne;
			ois = new ObjectInputStream( new FileInputStream("hans") );
			anotherOne = (Person) ois.readObject();

			System.out.println( "object retrieved from file:\n"+ anotherOne.toString() );
		} catch (IOException ioe) {
			System.out.println("an IOException occurred while reading back object");
		} catch (ClassNotFoundException cnfe) {
			System.out.println("could not find class Person");
		} finally {
			try {
				ois.close();
			} catch (IOException ioe) {
				//ignore it
			} //catch
		} //finally
	} //main()
} //class serializeData

class Person implements Serializable {
	public int yearOfBirth;
	public String name;
	transient int age;

	public void finalize() {
		System.out.println("object destroyed");
	} //finalize()

	public String toString() {
		return("name="+name+"\n"+"yearOfBirth="+yearOfBirth+"\n"+"age="+age);
	}//toString()
} //class Person