序列化

2017-08-24  本文已影响4人  萤火之森ss
package io;

import com.car.service.model.Question;

import java.io.*;

/**
 * Created by Wangjianxin on 2017/8/24 0024.
 */
public class Objectoutstrean {

    public static void main(String[] args) throws IOException{

        String f = "C:\\Users\\Administrator\\Desktop\\filetest\\ser.txt";

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
        Question question = new Question();
        question.setQuestitle("序列化");
        oos.writeObject(question);
        oos.flush();
        oos.close();


        
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        try {
            Question question1 = (Question)ois.readObject();
            System.out.println(question1);
            ois.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

关键字tansient,修饰后代表不能被默认的序列化操作,但是我们可以手动的序列化操作
对子类进行反序列操作时,如果父类没有实现序列化接口,那么父类的构造方法才会被调用

上一篇下一篇

猜你喜欢

热点阅读