2010-11-07 12 views

उत्तर

22

defaultReadObject() डिफ़ॉल्ट अक्रमांकन तंत्र का आह्वान, और प्रयोग किया जाता है जब आप अपने Serializable वर्ग पर readObject() विधि परिभाषित करते हैं। दूसरे शब्दों में, जब आपके पास कस्टम deserialization तर्क है, तो आप अभी भी डिफ़ॉल्ट क्रमिकरण पर वापस आ सकते हैं, जो आपके गैर स्थैतिक, गैर-क्षणिक फ़ील्ड को deserialize करेगा। उदाहरण के लिए:

public class SomeClass implements Serializable { 
    private String fld1; 
    private int fld2; 
    private transient String fld3; 
    private void readObject(java.io.ObjectInputStream stream) 
     throws IOException, ClassNotFoundException { 
     stream.defaultReadObject(); //fills fld1 and fld2; 
     fld3 = Configuration.getFooConfigValue(); 
    } 
] 

दूसरी ओर, readObject() प्रयोग किया जाता है जब आप deserialized वस्तु से ObjectInputStream बनाने के लिए, बाहर से, और एक वस्तु है कि पहले धारावाहिक में पढ़ना चाहते हैं:

ObojectInputStream stream = new ObjectInputStream(aStreamWithASerializedObject); 
Object foo = (Foo) stream.readObject(); 
+0

EOO अपवाद फेंकने को पढ़ने के लिए कैसे करें? –

+0

@ एजाज़ आप नहीं कर सकते। बस इसे पकड़ो। – Bozho

संबंधित मुद्दे