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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。