Results 1 to 3 of 3

Thread: Serialization

  1. #1
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Serialization

    Below is my code I have, which writes objects to my file, it then reopens file
    and reads the objects, then prints out the objects.

    Java Code:
    import java.io.*;
    
    /**
     * The object to serialize.
     */
    class ObjectToSerialize implements Serializable {
    
        private String name;
        private String address;
        private String phone;
    
        public ObjectToSerialize(String name, String address, String phone) {
            this.name = name;
            this.address = address;
            this.phone = phone;
        }
    
        // Default constructor
        public ObjectToSerialize() {
        }
    
        @Override
        public String toString() {
            return name + " lives at: " + address + ", his phone number is: " + phone;
        }
    }
    
    public class HasAProfile {
    
        private static final String FILE_NAME = "objects.ser";
    
        public static void main(String[] args) {
            try {
                // create the object to be serialized
                //ObjectToSerialize ots = new ObjectToSerialize("John Doe", "123 N 1st St", "555-5555");
                ObjectToSerialize ots = new ObjectToSerialize();
                // create the target File object and erase any already existing file
                File objectFile = new File(FILE_NAME);
                if (objectFile.exists()) {
                    objectFile.delete();
                }
    
                try {
                    // open the file, create the output stream, and write the object
                    FileOutputStream fos = new FileOutputStream(objectFile);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    oos.writeObject(ots);
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                try {
                    // reopen the file and retrieve the object
                    FileInputStream fis = new FileInputStream(objectFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    ObjectToSerialize retrieved = (ObjectToSerialize) ois.readObject();
                    System.out.println(retrieved);
                    ois.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    However, if I don't create the objects with info, like this:

    Java Code:
    ObjectToSerialize ots = new ObjectToSerialize("John Doe", "123 N 1st St", "555-5555");
    and create object with default constructor, I get the following output:

    Java Code:
    null lives at: null, his phone number is: null
    What I want to do is, if the object that I am trying to deserialize contains
    null objects, then I want to prompt the user for name, address, and phone
    number, and then use this information to create a new serialized profile on
    disk. Any ideas on how I can accomplish that?
    Last edited by twiggy62; 02-13-2010 at 09:13 AM.
    The biggest room in the world, is room for improvement.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Hehe ... so simple.

    Thank you!
    The biggest room in the world, is room for improvement.

Similar Threads

  1. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 04:03 PM
  2. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  3. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 09:13 AM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •