Here are my class:

public class MyClassToBePersisted implements Serializable {

    String profile, school;
    transient private int yearStarted;

    public void Profile(String name, int age, String hobby) {
        profile = name + " " + age + " " + hobby;
    }

    public void School(String nameOfSchool, int yearStarted) {
        this.yearStarted = yearStarted;
        school = nameOfSchool + " " + yearStarted;
    }

    public String getProfile() {
        return profile + school;
    }
}

public class SerializeMyClassToBePersisted {

    public static void main(String[] args) {

        String filename = "sample.txt";
        if (args.length > 0) {
            filename = args[0];
        }

        MyClassToBePersisted mc = new MyClassToBePersisted();
        mc.Profile("Mike", 14, "Swimming");
        mc.School("Poly", 1998);

        FileOutputStream fos = null;
        ObjectOutputStream out = null;

        try {
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(mc);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(mc.getProfile());
    }
}

public class DeserializeMyClassToBePersisted {

    public static void main(String[] args) {

        String filename = "sample.txt";
        if (args.length > 0) {
            filename = args[0];
        }

        // Deserialize the previously saved
        // PersistentTime object instance.
        MyClassToBePersisted mc = null;
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            mc = (MyClassToBePersisted) in.readObject();
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        System.out.println(mc.getProfile());
    }
}


When I run the SerializeMyClassToBePersisted.java I get file with
unreadable and readable text.
When I run the DeserializeMyClassToBePersisted.java It reads it
correctly. My problem is the yearStarted is transient and should be
null after it gets deserialized right? I'm not getting that.


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to