描述
這個java.io.ObjectOutputStream.PutField.put(String name, short val)方法將命名的短字段的值放入持久字段。
聲明
以下是聲明java.io.ObjectOutputStream.PutField.put()方法。
public abstract void put(String name, short val)
參數
name- 可序列化字段的名稱。
val− 分配給字段的值。
返回值
此方法不返回值。
異常
IllegalArgumentException- 如果名稱與正在寫入其字段的類的可序列化字段的名稱不匹配,或者如果命名字段的類型不短。
示例
下麵的例子展示了使用java.io.ObjectOutputStream.PutField.put()方法。
package com.tutorialspoint;
import java.io.*;
public class ObjectOutputStreamDemo implements Serializable {
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();
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 short var = 5;
// assign a new serialPersistentFields
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("var", Short.TYPE)
};
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
// get the field and assign it at var
ObjectInputStream.GetField fields = in.readFields();
// get var
short d = 87;
var = fields.get("var", d);
}
private void writeObject(ObjectOutputStream out) throws IOException {
// write into the ObjectStreamField array the variable var
ObjectOutputStream.PutField fields = out.putFields();
fields.put("var", var);
out.writeFields();
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
5
相關用法
- Java Java.io.ObjectOutputStream.writeObject()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeShort()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeUnshared()用法及代碼示例
- Java Java.io.ObjectOutputStream.replaceObject()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeBytes()用法及代碼示例
- Java Java.io.ObjectOutputStream.reset()用法及代碼示例
- Java Java.io.ObjectOutputStream.putFields()用法及代碼示例
- Java Java.io.ObjectOutputStream.close()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeDouble()用法及代碼示例
- Java Java.io.ObjectOutputStream.flush()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeLong()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeFloat()用法及代碼示例
- Java Java.io.ObjectOutputStream.drain()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeByte()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeBoolean()用法及代碼示例
- Java Java.io.ObjectOutputStream.annotateClass()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeChars()用法及代碼示例
- Java Java.io.ObjectOutputStream.write()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeInt()用法及代碼示例
- Java Java.io.ObjectOutputStream.writeChar()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.io.ObjectOutputStream.PutField.put() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。