ObjectInputStream 类的 readObject() 方法用于从 objectinputstream 中读取一个对象。它读取对象的类、类的签名、静态、非静态字段和超类型。
用法
public final Object readObject() throws IOException, ClassNotFoundException
参数
NA
返回
从流中读取的对象。
抛出
类未找到异常
无效类异常
流损坏异常
可选数据异常
IOException
例子1
import java.io.*;
import static java.lang.System.out;
public class ObjectInputStreamreadObjectExample1 {
public static void main(String[] args) {
try {
String str1 = "JavaTpoint";
FileOutputStream outstream = new FileOutputStream("file1.txt");
ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
objoutstream.writeObject(new WorkClass());
objoutstream.writeObject(str1);
objoutstream.flush();
ObjectInputStream objinstream = new ObjectInputStream(new FileInputStream("file1.txt"));
WorkClass obj = (WorkClass) objinstream.readObject();
System.out.println("String in Serializable Class :" + obj.str);
System.out.println("String in MainClass : " +str1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class WorkClass implements Serializable {
String str = "Welcome to javaTpoint";
private String readObject(ObjectInputStream objin)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField g = objin.readFields();
return (String) g.get("str", null);
}
}
}
输出:
String in Serializable Class :Welcome to javaTpoint String in MainClass : JavaTpoint
例子2
package ObjectInputStream;
import java.io.*;
import java.io.ObjectInputStream;
public class ObjectInputStreamreadObjectExample2 {
public static void main(String[] args) {
try {
FileOutputStream outstream = new FileOutputStream("file1.txt");
ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
FileInputStream instream = new FileInputStream("file2.txt");
ObjectInputStream objinstream = new ObjectInputStream(instream);
String str=objinstream.toString();
objoutstream.writeObject(new WorkClass());
objoutstream.writeObject(str);
objoutstream.flush();
ObjectInputStream objinputstram = new ObjectInputStream(new FileInputStream("file1.txt"));
//read the object from object input stream and cast it to String type.
System.out.println("" + (String) objinstream.readObject());
//read the object from object input stream and cast it to workclass typr.
WorkClass obj1 = (WorkClass) objinputstram.readObject();
System.out.println("string in Work class :" + obj1.str1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class WorkClass implements Serializable {
String str1 = "javaTpoint";
private void readObject(ObjectInputStream objin)
throws IOException, ClassNotFoundException {
objin.defaultReadObject();
}
}
}
输出:
[email protected] string in Work class :javaTpoint
相关用法
- Java ObjectInputStream readObject()用法及代码示例
- Java ObjectInputStream readObjectOverride()用法及代码示例
- Java ObjectInputStream readDouble()用法及代码示例
- Java ObjectInputStream readUnsignedByte()用法及代码示例
- Java ObjectInputStream readFields()用法及代码示例
- Java ObjectInputStream readFully()用法及代码示例
- Java ObjectInputStream readInt()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- Java ObjectInputStream readUTF()用法及代码示例
- Java ObjectInputStream readField()用法及代码示例
- Java ObjectInputStream readShort()用法及代码示例
- Java ObjectInputStream readUnsingedByte()用法及代码示例
- Java ObjectInputStream readByte()用法及代码示例
- Java ObjectInputStream readLong()用法及代码示例
- Java ObjectInputStream readStreamHeader()用法及代码示例
- Java ObjectInputStream readUnsignedShort()用法及代码示例
- Java ObjectInputStream readUnshared()用法及代码示例
- Java ObjectInputStream readChar()用法及代码示例
- Java ObjectInputStream readLine()用法及代码示例
- Java ObjectInputStream readBoolean()用法及代码示例
注:本文由纯净天空筛选整理自 Java ObjectInputStream readObject() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。