参数

返回值

异常

例子

下面的示例演示java.io.ObjectOutputStream.PutField.put()方法的用法。

package com.yiibai;import java.io.*;publicclassObjectOutputStreamDemoimplementsSerializable{publicstaticvoid main(String[] args){try{// create a new file with an ObjectOutputStreamFileOutputStreamout=newFileOutputStream("test.txt");ObjectOutputStream oout =newObjectOutputStream(out);// write something in the file
oout.writeObject(newExample());
oout.flush();
oout.close();// create an ObjectInputStream for the file we created beforeObjectInputStream ois =newObjectInputStream(newFileInputStream("test.txt"));// read an object from the stream and cast it to ExampleExample a =(Example) ois.readObject();// print var of aSystem.out.println(""+ a.var);}catch(Exception ex){
ex.printStackTrace();}}staticpublicclassExampleimplementsSerializable{staticshortvar=5;// assign a new serialPersistentFields privatestaticfinalObjectStreamField[] serialPersistentFields ={newObjectStreamField("var",Short.TYPE)};privatevoid readObject(ObjectInputStreamin)throwsIOException,ClassNotFoundException{// get the field and assign it at varObjectInputStream.GetField fields =in.readFields();// get varshort d =87;var= fields.get("var", d);}privatevoid writeObject(ObjectOutputStreamout)throwsIOException{// write into the ObjectStreamField array the variable varObjectOutputStream.PutField fields =out.putFields();
fields.put("var",var);out.writeFields();}}}