<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 外部化示例 – 更有效的序列化 > 原文: [https://howtodoinjava.com/java/serialization/java-externalizable-example/](https://howtodoinjava.com/java/serialization/java-externalizable-example/) 默認的 Java 序列化效率不高。 如果您序列化了一個具有許多屬性和屬性的胖對象,則您不希望出于任何原因進行序列化(例如,它們始終被分配默認值),您將需要處理繁重的對象并通過網絡發送更多字節,這在某些情況下可能會非常昂貴。 要解決此問題,您可以通過實現[`Externalizable`](https://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html)接口并覆蓋其方法`writeExternal()`和`readExternal()`來編寫自己的序列化邏輯。 通過實現這些方法,您將告訴 JVM 如何編碼/解碼對象。 出于示例目的,我創建了這個簡單的類,我們將使用`writeExternal()`和`readExternal()`方法進行序列化和反序列化。 ```java class UserSettings implements Externalizable { //This is required public UserSettings(){ } private String doNotStoreMe; private Integer fieldOne; private String fieldTwo; private boolean fieldThree; public String getDoNotStoreMe() { return doNotStoreMe; } public void setDoNotStoreMe(String doNotStoreMe) { this.doNotStoreMe = doNotStoreMe; } public Integer getFieldOne() { return fieldOne; } public void setFieldOne(Integer fieldOne) { this.fieldOne = fieldOne; } public String getFieldTwo() { return fieldTwo; } public void setFieldTwo(String fieldTwo) { this.fieldTwo = fieldTwo; } public boolean isFieldThree() { return fieldThree; } public void setFieldThree(boolean fieldThree) { this.fieldThree = fieldThree; } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { fieldOne = in.readInt(); fieldTwo = in.readUTF(); fieldThree = in.readBoolean(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(fieldOne); out.writeUTF(fieldTwo); out.writeBoolean(fieldThree); } @Override public String toString() { return "UserSettings [doNotStoreMe=" + doNotStoreMe + ", fieldOne=" + fieldOne + ", fieldTwo=" + fieldTwo + ", fieldThree=" + fieldThree + "]"; } } ``` ## `Externalizable`的`writeExternal()`示例 [`writeExternal()`](https://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html#writeExternal%28java.io.ObjectOutput%29)方法用于提供序列化邏輯,即將類的字段寫入字節。 在讀回序列化的對象之后,您可以自由地僅存儲要返回的那些字段,忽略其余字段。 ```java public void writeExternal(ObjectOutput out) throws IOException { //We are not storing the field 'doNotStoreMe' out.writeInt(fieldOne); out.writeUTF(fieldTwo); out.writeBoolean(fieldThree); } ``` ## `Externalizable`的`readExternal()`示例 唯一需要記住的是 – [`readExternal()`](https://docs.oracle.com/javase/7/docs/api/java/io/Externalizable.html#readExternal%28java.io.ObjectInput%29)方法必須讀取與`writeExternal()`寫入的序列相同且類型相同的值。 ```java public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { fieldOne = in.readInt(); fieldTwo = in.readUTF(); fieldThree = in.readBoolean(); } ``` ## 完整的例子 現在,讓我們編寫代碼進行序列化并讀回字節,以驗證 JVM 是否遵守契約。 ```java public class ExternalizableExample { public static void main(String[] args) { UserSettings settings = new UserSettings(); settings.setDoNotStoreMe("Sensitive info"); settings.setFieldOne(10000); settings.setFieldTwo("HowToDoInJava.com"); settings.setFieldThree(false); //Before System.out.println(settings); storeUserSettings(settings); UserSettings loadedSettings = loadSettings(); System.out.println(loadedSettings); } private static UserSettings loadSettings() { try { FileInputStream fis = new FileInputStream("object.ser"); ObjectInputStream ois = new ObjectInputStream(fis); UserSettings settings = (UserSettings) ois.readObject(); ois.close(); return settings; } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } private static void storeUserSettings(UserSettings settings) { try { FileOutputStream fos = new FileOutputStream("object.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(settings); oos.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` ```java Output: UserSettings [doNotStoreMe=Sensitive info, fieldOne=10000, fieldTwo=HowToDoInJava.com, fieldThree=false] UserSettings [doNotStoreMe=null, fieldOne=10000, fieldTwo=HowToDoInJava.com, fieldThree=false] ``` 顯然,我們可以取回所需的字段,而忽略不需要的字段,這就是`Externalizable`接口的全部目的。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看