一道關於java序列化的問題,看大家知多少————

來源:互聯網
上載者:User

標籤:src   bin   技術分享   多少   rgs   art   version   alt   not   

 

問題先放在這裡,稍後我會做出解答

 

已知類有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,string65535,null第二個主程式:65535,hello255,hello*/
答案

 

一道關於java序列化的問題,看大家知多少————

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.