當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ObjectInputStream defaultReadObject()用法及代碼示例


Java中ObjectInputStream類的defaultReadObject()方法用於從此流中讀取當前類的非靜態和非瞬態字段。

用法:

public void defaultReadObject()

參數:此方法不接受任何參數。

返回值:此方法返回已讀取的值。

錯誤和異常:該函數引發三個異常,如下所述:



  • ClassNotFoundException:如果找不到序列化對象的類,則會引發異常。
  • IOException:如果發生I /O錯誤,則會引發異常。
  • NotActiveException:如果流當前未讀取對象,則拋出異常。

以下示例程序旨在說明上述方法:

程序1:

Java

// Java program to illustrate 
// the above method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
            // create a new file 
            // with an ObjectOutputStream 
            FileOutputStream out 
                = new FileOutputStream("Shubham.txt"); 
            ObjectOutputStream out1 
                = new ObjectOutputStream(out); 
  
            // write 
            out1.writeObject(new solve()); 
  
            // Flushes the stream 
            out1.flush(); 
  
            // create an ObjectInputStream 
            // for the file 
            ObjectInputStream example 
                = new ObjectInputStream( 
                    new FileInputStream("Shubham.txt")); 
  
            // Read from the stream 
            solve ans = (solve)example.readObject(); 
            System.out.println(ans.str); 
        } 
  
        catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
  
    static class solve implements Serializable { 
        String str = "Geeksforgeeks"; 
  
        private void readObject(ObjectInputStream res) 
            throws IOException, 
                   ClassNotFoundException 
        { 
  
            // By using defaultReadObject() method is 
            // to read non-static fields of the present 
            // class from the ObjectInputStream 
            res.defaultReadObject(); 
        } 
    } 
}

輸出:

程序2:

Java

// Java program to illustrate 
// the above method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
            // create a new file 
            // with an ObjectOutputStream 
            FileOutputStream out 
                = new FileOutputStream("Shubham.txt"); 
            ObjectOutputStream out1 
                = new ObjectOutputStream(out); 
  
            // write 
            out1.writeObject(new solve()); 
  
            // Flushes the stream 
            out1.flush(); 
  
            // create an ObjectInputStream 
            // for the file 
            ObjectInputStream example 
                = new ObjectInputStream( 
                    new FileInputStream("Shubham.txt")); 
  
            // Read from the stream 
            solve ans = (solve)example.readObject(); 
            // System.out.println(ans.str); 
            System.out.println(ans.in); 
        } 
  
        catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
  
    static class solve implements Serializable { 
  
        // String str = "Geeksforgeeks"; 
        Integer in = new Integer(112414); 
  
        private void readObject(ObjectInputStream res) 
            throws IOException, 
                   ClassNotFoundException 
        { 
            // By using defaultReadObject() method is 
            // to read non-static fields of the present 
            // class from the ObjectInputStream 
            res.defaultReadObject(); 
        } 
    } 
}

輸出:

參考:

https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#defaultReadObject()




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 ObjectInputStream defaultReadObject() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。