描述
這個java.io.ObjectOutputStream.writeFields()方法將緩衝的字段傳遞給流。
聲明
以下是聲明java.io.ObjectOutputStream.writeFields()方法。
public void writeFields()
參數
NA
返回值
此方法不返回值。
異常
IOException− 如果在寫入底層流時發生 I/O 錯誤。
NotActiveException− 當未調用類 writeObject 方法來寫入對象的狀態時調用。
示例
下麵的例子展示了使用java.io.ObjectOutputStream.writeFields()方法。
package com.tutorialspoint;
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) {
double d = 1.59875d;
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();
oout.close();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read an object from the stream and cast it to Example
Example a = (Example) ois.readObject();
// print var of a
System.out.println("" + a.var);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static public class Example implements Serializable {
static int var = 76458;
// assign a new serialPersistentFields
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("var", Integer.TYPE)
};
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
// get the field and assign it at var
ObjectInputStream.GetField fields = in.readFields();
// get var
var = fields.get("var", 0);
}
private void writeObject(ObjectOutputStream out) throws IOException {
// write into the ObjectStreamField array the variable string
ObjectOutputStream.PutField fields = out.putFields();
fields.put("var", var);
out.writeFields();
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
76458
相關用法
- Java Java.io.ObjectOutputStream.writeFloat()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeObject()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeShort()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeUnshared()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeBytes()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeDouble()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeLong()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeByte()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeBoolean()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeChars()用法及代碼示例
- Java Java.io.ObjectOutputStream.write()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeInt()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeChar()用法及代碼示例
- Java Java.io.ObjectOutputStream.replaceObject()用法及代碼示例
- Java Java.io.ObjectOutputStream.reset()用法及代碼示例
- Java Java.io.ObjectOutputStream.putFields()用法及代碼示例
- Java Java.io.ObjectOutputStream.close()用法及代碼示例
- Java Java.io.ObjectOutputStream.flush()用法及代碼示例
- Java Java.io.ObjectOutputStream.drain()用法及代碼示例
- Java Java.io.ObjectOutputStream.annotateClass()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.ObjectOutputStream.writeFields() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。