序列化

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容