ObjectInputStream类readObject()方法 (ObjectInputStream Class readObject() method)
readObject() method is available in java.io package.
readObject()方法在java.io包中可用。
readObject() method is used to read an object from this ObjectInputStream and read the object in terms of object of the class, the signature of the class, values of non-static fields of the class, etc.
readObject()方法用于从此ObjectInputStream读取对象,并根据类的对象,类的签名,类的非静态字段的值等读取对象。
readObject() 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.
readObject()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
readObject() method may throw an exception at the time of the reading object.
readObject()方法在读取对象时可能会引发异常。
- ClassNotFoundException: This exception may throw when the serialized object Class could not exist.ClassNotFoundException :当序列化的对象Class不存在时,可能引发此异常。
- InvalidClassException: This exception may throw when the invalid class used by serialization.InvalidClassException :当序列化使用无效的类时,可能引发此异常。
- StreamCorruptedException: This exception may throw when the operating information in the stream is not consistent.StreamCorruptedException :当流中的操作信息不一致时,可能引发此异常。
- IOException: This exception may throw when getting any input/output error while performing.IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。
Syntax:
句法:
public Object readObject();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is Object, it returns the Object read from the ObjectInputStream.
方法的返回类型为Object ,它返回从ObjectInputStream读取的Object。
Example:
例:
// Java program to demonstrate the example
// of Object readObject() method of ObjectInputStream
import java.io.*;
public class ReadObjectClass {
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.readObject(): " + def_obj.str);
}
static class DefaultObjectClass implements Serializable {
String str = "Java World";
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.readObject(): Java World
翻译自: https://www.includehelp.com/java/objectinputstream-readobject-method-with-example.aspx