📜  Java中的 ObjectInputStream defaultReadObject() 方法及示例

📅  最后修改于: 2022-05-13 01:55:08.097000             🧑  作者: Mango

Java中的 ObjectInputStream defaultReadObject() 方法及示例

Java中ObjectInputStream类的defaultReadObject()方法用于从这个流中读取当前类的非静态和非瞬态字段。

句法:

public void defaultReadObject()

参数:此方法不接受任何参数。

返回值:该方法返回已读取的值。

错误和异常:该函数抛出三个异常,如下所述:

  • ClassNotFoundException:如果找不到序列化对象的类,则抛出异常。
  • IOException:如果发生 I/O 错误,则抛出异常。
  • NotActiveException:如果流当前没有读取对象,则抛出异常。

下面的程序说明了上述方法:

方案一:

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();
        }
    }
}


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();
        }
    }
}


输出:

方案二:

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 Java()