描述
這個java.io.ObjectInputStream.readFields()方法從流中讀取持久字段並按名稱使它們可用。
聲明
以下是聲明java.io.ObjectInputStream.readFields()方法。
public ObjectInputStream.GetField readFields()
參數
NA
返回值
google.gr此方法返回表示正在反序列化的對象的持久字段的 GetField 對象。
異常
ClassNotFoundException− 如果找不到序列化對象的類。
IOException− 如果發生 I/O 錯誤。
NotActiveException− 如果流當前未讀取對象。
示例
下麵的例子展示了使用java.io.ObjectInputStream.readFields()方法。
package com.tutorialspoint;
import java.io.*;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
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(new Example());
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read the object and print the string
Example a = (Example) ois.readObject();
// print the string that is in Example class
System.out.println("" + a.s);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class Example implements Serializable {
String s = "Hello World!";
private String readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
// call readFields in readObject
ObjectInputStream.GetField gf = in.readFields();
// save the string and return it
return (String) gf.get("s", null);
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Hello World!
相關用法
- Java Java.io.ObjectInputStream.readFloat()用法及代碼示例
- Java Java.io.ObjectInputStream.readFully()用法及代碼示例
- Java Java.io.ObjectInputStream.readObject()用法及代碼示例
- Java Java.io.ObjectInputStream.readInt()用法及代碼示例
- Java Java.io.ObjectInputStream.readChar()用法及代碼示例
- Java Java.io.ObjectInputStream.readUTF()用法及代碼示例
- Java Java.io.ObjectInputStream.readBoolean()用法及代碼示例
- Java Java.io.ObjectInputStream.read()用法及代碼示例
- Java Java.io.ObjectInputStream.readUnshared()用法及代碼示例
- Java Java.io.ObjectInputStream.readLong()用法及代碼示例
- Java Java.io.ObjectInputStream.readDouble()用法及代碼示例
- Java Java.io.ObjectInputStream.readShort()用法及代碼示例
- Java Java.io.ObjectInputStream.readByte()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveClass()用法及代碼示例
- Java Java.io.ObjectInputStream.resolveObject()用法及代碼示例
- Java Java.io.ObjectInputStream.getField.get()用法及代碼示例
- Java Java.io.ObjectInputStream.skipBytes()用法及代碼示例
- Java Java.io.ObjectInputStream.available()用法及代碼示例
- Java Java.io.ObjectInputStream.close()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeObject()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.ObjectInputStream.readFields() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。