Java self-study notes (20): [IO] object flow

Object serialization and de-serialization

Converting the serialized object sequence of bytes called objects, Serialization // save the state of the object can be read again

The process for the recovery target sequence of bytes called object deserialization, Deserialization    

Applications: The target byte sequence permanently saved to the hard disk, the sequence of bytes transmitted on the network object, the object passed in the serialization process

Only objects that implement the Serializable interface and Externalizable class to be serialized

Serializable interface is a marker interface Public interface Serializable {}

Externalizable interface inherits from the Serializable

 

Object output stream

Serialization ObjectOutputStream class for the object, the object is about to output

//Person.java
package object;

import java.io.Serializable;

public class Person implements Serializable {  //序列化
    /**
     * 
     */
    private static final long serialVersionUID = 1L;  //序列化的ID标识
    public String name;
    public int age;
}
//ObjectOutputStreamDemo
package object;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {

    public static void main(String[] args) {
        File file = new File("Person.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            Person person = new Person();
            person.name= "tkj";
            person.age=17;
            oos.writeObject(person);
            oos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }    
         catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Object input stream

ObjectInputStream

package object;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {

    public static void main(String[] args) {   //读取刚刚的Person
        File file = new File("Person.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            Person person = new Person();
            person.name= "tkj";
            person.age=17;
            oos.writeObject(person);
            oos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }    
         catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

transient keyword: domain is not used to represent a portion of the target sequence, the variable is not modified serialized, static have such a role


 

So hard ah ~

 


 

Guess you like

Origin www.cnblogs.com/tkj521Ya/p/11287848.html