Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 3 Times in 3 Posts

    ObjectOutputStream, ObjectInputStream, writeExternal, readExternal Example

    I though about sharing an example about

    ObjectOutputStream
    ObjectInputStream
    writeExternal
    readExternal.

    This example consists of four classes

    ObjectInOut.java - Main class
    Student.java - Student class (One Name and One Address object)
    Name.java
    Address.java


    ObjectInOut.java


    Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class ObjectInOut {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
    
            /*
             * ObjectOutputStream and  ObjectInputStream
             * objects initialized
             */
            ObjectOutputStream oOut = new ObjectOutputStream(new FileOutputStream("student.dat"));
    
            Student s1 = new Student(new Name(Name.MR, "Rodrigo", "Al", "Domínguez"), new Address("Av. Bellavista N 185", "New York", "NY", "12345", "US"));
    
            Student s2 = new Student(new Name(Name.DR, "Kuldeep", "Kalli", "Manak"), new Address("563 Model Town", "Ludhiana", "PB", "161601", "India"));
    
            try {
                //writing to file
                oOut.writeObject(s1);
                oOut.writeObject(s2);
            } finally {
                //closing output file
                oOut.close();
            }
    
            s1 = s2 = null;
    
            ObjectInputStream oIn = new ObjectInputStream(new FileInputStream("student.dat"));
    
            try {
                //reading from file
                s1 = (Student) oIn.readObject();
                s2 = (Student) oIn.readObject();
            } finally {
                //closing input file
                oIn.close();
            }
    
            //display results
            System.out.printf("%s\n%s", s1.toString(), s2.toString());
    
    
        }
    }

    Student.java


    Code:
    import java.io.Externalizable;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectOutput;
    import java.io.Serializable;
    
    public class Student implements Serializable, Externalizable {
    
        //no argument constructor
        public Student() {
        }
    
        //argument constructor
        public Student(Name SName, Address SAddress) {
            this.SName = SName;
            this.SAddress = SAddress;
        }
    
        private Name SName;
        private Address SAddress;
    
        public Name getSName() {
            return SName;
        }
    
        public void setSName(Name SName) {
            this.SName = SName;
        }
    
        public Address getSAddress() {
            return SAddress;
        }
    
        public void setSAddress(Address SAddress) {
            this.SAddress = SAddress;
        }
    
        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeObject(this.SName); //calles writeExternal() in Name class
            out.writeObject(this.SAddress); //calles writeExternal() in Address class
        }
    
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            this.setSName((Name) in.readObject()); //calles readExternal() in Name class
            this.setSAddress((Address) in.readObject()); //calles readExternal() in Address class
        }
    
        @Override
        public String toString() {
            return String.format("%s\n%s", SName.toString(), SAddress.toString());
        }
    }

    Name.java


    Code:
    import java.io.Externalizable;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectOutput;
    import java.io.Serializable;
    
    public class Name implements Serializable, Externalizable {
    
        //no argument constructor
        public Name() {
    
        }
    
        //argument constructor
        public Name(String honorific, String first, String middle, String last) {
            this.honorific = honorific;
            this.first = first;
            this.middle = middle;
            this.last = last;
        }
    
        public static final String MR = "Mr.";
        public static final String MRS = "Mrs.";
        public static final String MS = "Ms.";
        public static final String DR = "Dr.";
        public static final String NONE = "";
    
        private String honorific;
        private String first;
        private String middle;
        private String last;
    
        //getters and setters
        public String getHonorific() {
            return honorific;
        }
    
        public void setHonorific(String honorific) {
            this.honorific = honorific;
        }
    
        public String getFirst() {
            return first;
        }
    
        public void setFirst(String first) {
            this.first = first;
        }
    
        public String getMiddle() {
            return middle;
        }
    
        public void setMiddle(String middle) {
            this.middle = middle;
        }
    
        public String getLast() {
            return last;
        }
    
        public void setLast(String last) {
            this.last = last;
        }
    
        //defines the output structure
        public void writeExternal(ObjectOutput out) throws IOException {
            /*
             * Use writeUTF instead of writeChar/writeBytes to
             * be ASCII and Unicode independent along with
             * memory efficiency.
             *
             * More Info
             * http://download.oracle.com/javase/1.4.2/docs/api/java/io/DataOutput.html#writeUTF(java.lang.String)
             */
    
            out.writeUTF(this.getHonorific());
            out.writeUTF(this.getFirst());
            out.writeUTF(this.getMiddle());
            out.writeUTF(this.getLast());
        }
    
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            /*
             * readUTF used beacuse we used writeUTF before
             */
    
    
            this.setHonorific(in.readUTF());
            this.setFirst(in.readUTF());
            this.setMiddle(in.readUTF());
            this.setLast(in.readUTF());
        }
    
        @Override
        public String toString() {
            if (this.getHonorific().equals(NONE)) {
                return String.format("%s %s %s", first, middle, last);
    
            } else {
                return String.format("%s %s %s %s", honorific, first, middle, last);
            }
        }
    }

    Address.java


    Code:
    import java.io.Externalizable;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectOutput;
    import java.io.Serializable;
    
    public class Address implements Serializable, Externalizable {
    
        private String street;
        private String city;
        private String province;
        private String zipCode;
        private String country;
    
        //no argument constructor
        public Address() {
        }
    
        //argument constructor
        public Address(String street, String city, String province, String zipCode, String country) {
            this.street = street;
            this.city = city;
            this.province = province;
            this.zipCode = zipCode;
            this.country = country;
        }
    
        //getters and setters
        public String getStreet() {
            return street;
        }
    
        public void setStreet(String street) {
            this.street = street;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getProvince() {
            return province;
        }
    
        public void setProvince(String province) {
            this.province = province;
        }
    
        public String getZipCode() {
            return zipCode;
        }
    
        public void setZipCode(String zipCode) {
            this.zipCode = zipCode;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        public void writeExternal(ObjectOutput out) throws IOException {
            /*
             * Use writeUTF instead of writeChar/writeBytes to
             * be ASCII and Unicode independent along with
             * memory efficiency.
             *
             * More Info
             * http://download.oracle.com/javase/1.4.2/docs/api/java/io/DataOutput.html#writeUTF(java.lang.String)
             */
    
    
            out.writeUTF(this.getStreet());
            out.writeUTF(this.getCity());
            out.writeUTF(this.getProvince());
            out.writeUTF(this.getZipCode());
            out.writeUTF(this.getCountry());
        }
    
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            /*
             * readUTF used beacuse we used writeUTF before
             */
    
            this.setStreet(in.readUTF());
            this.setCity(in.readUTF());
            this.setProvince(in.readUTF());
            this.setZipCode(in.readUTF());
            this.setCountry(in.readUTF());
    
        }
    
        @Override
        public String toString() {
            return String.format("%s %s %s %s %s",street, city, province,zipCode,country);
        }
    }
    Hoping being helpful.

    Attached .java available
    Attached Files Attached Files


 

Tags for this Thread

Posting Permissions

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