一道关于java序列化的问题,看大家知多少————

 

问题先放在这里,稍后我会做出解答

 

已知类有Test和Test2,问两次主程序的输出结果是多少(SerializeUtil只是序列化的工具类)

类Test

public class Test implements Serializable{
    private static final long serialVersionUID = 1L;
    private int num;
    private transient String str;

    public Test(){
        this.num = 0xFFFF;
        this.str = "string";
    }

    public Test(int num, String str) {
        this.num = num;
        this.str = str;
    }

    @Override
    public String toString() {
        return num + ","+str;
    }
}

类Test2

public class Test2 implements Externalizable,Serializable{
    private static final long serialVersionUID = 1L;
    private int num;
    private transient String str;

    public Test2() {
        this.num = 0xFFFF;
        this.str = "hello";
    }

    public Test2(int num, String str) {
        this.num = num;
        this.str = str;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.write(num);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.num = in.read();
    }


    @Override
    public String toString() {
        return num + ","+str;
    }
}

两个主程序:

    public static void main(String[] args) {
        Test test = new Test();
        File file = new File("d:/test.bin");
        SerializeUtil.writeObject(file,test);

        Test test2 = (Test) SerializeUtil.readObject(file);
        System.out.println(test);
        System.out.println(test2);
    }

 

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        File file = new File("d:/test2.bin");

        SerializeUtil.writeObject(file,test2);
        Test2 t = (Test2) SerializeUtil.readObject(file);

        System.out.println(test2);
        System.out.println(t);
    }

 

附SerializeUtil的代码,可无视——

 1 public class SerializeUtil {
 2 
 3     public static void writeObject(File file,Object object){
 4         FileOutputStream fileOutputStream = null;
 5         try {
 6             fileOutputStream = new FileOutputStream(file);
 7             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
 8             objectOutputStream.writeObject(object);
 9             objectOutputStream.flush();
10         }catch (IOException e){
11             e.printStackTrace();
12         }finally {
13             if(fileOutputStream!=null){
14                 try {
15                     fileOutputStream.close();
16                 } catch (IOException e) {}
17             }
18         }
19     }
20 
21     public static Object readObject(File file){
22         FileInputStream fileInputStream = null;
23         try {
24             fileInputStream = new FileInputStream(file);
25             ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
26             return objectInputStream.readObject();
27         }catch (ClassNotFoundException|IOException e){
28             e.printStackTrace();
29         }finally {
30             if(fileInputStream!=null){
31                 try {
32                     fileInputStream.close();
33                 } catch (IOException e) {}
34             }
35         }
36         return null;
37     }
38 
39 }
序列化工具类

 

答案如下:

/*
第一个主程序:
65535,string
65535,null
第二个主程序:
65535,hello
255,hello
*/
答案

 

转载于:https://www.cnblogs.com/xcr1234/p/6071083.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值