Java foundation classes of objects IO stream and serialization

Two stream class object:

  ObjectOutputStream: the basic data types Java objects and graphics written OutputStream

  ObjectInputStream: basic data and objects previously written using an ObjectOutputStream deserialized

Sequence of a set of objects:

  In series operation, when multiple objects simultaneously serialized, deserialized must operate in sequence, if you want to serialize a set of objects, how does it work?

Sequence of a set of objects may take the form of an array of objects, because the object array can transition operation to the Object.

  The target sequence to which the file, although the file is stored in bytes. Conversion values ​​which we like objects associated attribute bytes written to the file, a process called serialization

  Deserialization process: the document object properties and the like, read out, the byte swap object

transicent Keywords:

  If a transient instance variables declared, when an object is stored, its value need not be maintained.

Dog.java:
package IODemo;

import java.io.Serializable;

/ ** If you create a class object needs to be serialized, the class must implement Serializable
 * Serializable is a marker interface, there is no defined, to tell the JVM class object can be serialized
 * When an object needs to be serialized it?
 * 1, the object is saved to a file (stored in the physical medium)
 * 2, the object need to be transmitted over the network
 * / 
// to be transmitted if the object does not serialize (the Serializable), then the error will java.io.NotSerializableException 
public  class Dog   the implements the Serializable {
     Private String name;
     Private  int Age;
     Private String Sex;
    // Private transient int ID; If you use the transient keyword, indicating that this value can be ignored when serializing

    public Dog(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Dog() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

 

ObjectStreamDemo.java :
package IODemo;

import java.io. * ;

/**
 * When the transmission is the use of multiple objects when the array of objects. Pre-existent to the inside and then traverse read!
 *
 */
public class ObjectStreamDemo {
    private static void ObjectRead(){ File file = new File ( "d: \\ test \\ test.obj"); // extensions arbitrarily set
        try {
            InputStream in = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(in); Dog dog = (Dog) ois.readObject(); System.out.println(dog.toString()); ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static void ObjectWrite(){ Dog dog = new Dog("旺旺",2,"公"); File file = new File("d:\\test\\test.obj"); //扩展名 随意定 try { OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(dog); oos.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ObjectWrite(); ObjectRead(); } }

 

 

Guess you like

Origin www.cnblogs.com/lpss-75074038/p/11980378.html