Java.io.ObjectInputStream readObjectOverride() 方法
描述
java.io.ObjectInputStream.readObjectOverride() 方法由使用受保护的无参数构造函数构造 ObjectOutputStream 的 ObjectOutputStream 的受信任子类调用。 子类应提供带有修饰符"final"的覆盖方法。
声明
以下是 java.io.ObjectInputStream.readObjectOverride() 方法的声明。
protected Object readObjectOverride()
参数
NA
返回值
该方法返回从流中读取的对象。
异常
ClassNotFoundException − 找不到序列化对象的类。
OptionalDataException − 在流中找到原始数据而不是对象。
IOException − 如果从底层流读取时发生 I/O 错误
示例
下面的例子展示了 java.io.ObjectInputStream.readObjectOverride() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class ObjectInputStreamDemo extends ObjectInputStream{
public ObjectInputStreamDemo(InputStream in) throws IOException {
super(in);
}
public static void main(String[] args) {
String s = "Hello World";
try {
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeObject(s);
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStreamDemo ois = new ObjectInputStreamDemo(new FileInputStream("test.txt"));
// read and print an object and cast it as string
System.out.println("" + (String)ois.readObjectOverride());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
null

