ObjectInputStream类defaultReadObject()方法 (ObjectInputStream Class defaultReadObject() method)
defaultReadObject() method is available in java.io package.
defaultReadObject()方法在java.io包中可用。
defaultReadObject() method is used to read the non-static and non-transient field of the current class from this ObjectInputStream and it involves indirectly with the help of readObject() method of this class.
defaultReadObject()方法用于从此ObjectInputStream读取当前类的非静态和非瞬态字段,并且在此类的readObject()方法的帮助下间接涉及此方法。
defaultReadObject() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
defaultReadObject()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
defaultReadObject() method may throw an exception at the time of reading the default object.
在读取默认对象时, defaultReadObject()方法可能会引发异常。
IOException: This exception may throw when getting any input/output error while performing.
IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。
Syntax:
句法:
public void defaultReadObject();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is void, it returns nothing.
该方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of void defaultReadObject() method
// of ObjectInputStream
import java.io.*;
public class DefaultReadObject {
public static void main(String[] args) throws Exception {
// Instantiates ObjectOutputStream , ObjectInputStream
// FileInputStream and FileOutputStream
FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
// By using writeObject() method is to
// write object to Serialized class
obj_out_stm.writeObject(new DefaultObjectClass());
obj_out_stm.flush();
// By using readObject() method is to
// read an object from the Serialized class
DefaultObjectClass def_obj = (DefaultObjectClass) obj_in_stm.readObject();
// Using both readObject and defaultReadObject();
System.out.println("obj_in_stm.defaultReadObject(): " + def_obj.str);
System.out.println("obj_in_stm.defaultReadObject(): " + def_obj.in);
}
static class DefaultObjectClass implements Serializable {
String str = "Java Programming";
Integer in = new Integer(10);
private void readObject(ObjectInputStream obj_stm) throws IOException, ClassNotFoundException {
// By using defaultReadObject() method is
// to read non-static fields of the present
// class from the ObjectInputStream
obj_stm.defaultReadObject();
}
}
}
Output
输出量
obj_in_stm.defaultReadObject(): Java Programming
obj_in_stm.defaultReadObject(): 10
翻译自: https://www.includehelp.com/java/objectinputstream-defaultreadobject-method-with-example.aspx