ObjectInputStream 和 ObjectOutputStream 介绍_动力节点Java学院整理
ObjectInputStream和ObjectOutputStream的作用是,对基本数据和对象进行序列化操作支持。
创建“文件输出流”对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对“基本数据或对象”的持久存储;当我们需要读取这些存储的“基本数据或对象”时,可以创建“文件输入流”对应的ObjectInputStream,进而读取出这些“基本数据或对象”。
注意:只有支持java.io.Serializable或java.io.Externalizable接口的对象才能被ObjectInputStream/ObjectOutputStream所操作!
ObjectOutputStream函数列表
//构造函数 ObjectOutputStream(OutputStreamoutput) //public函数 voidclose() voiddefaultWriteObject() voidflush() ObjectOutputStream.PutFieldputFields() voidreset() voiduseProtocolVersion(intversion) voidwrite(intvalue) voidwrite(byte[]buffer,intoffset,intlength) voidwriteBoolean(booleanvalue) voidwriteByte(intvalue) voidwriteBytes(Stringvalue) voidwriteChar(intvalue) voidwriteChars(Stringvalue) voidwriteDouble(doublevalue) voidwriteFields() voidwriteFloat(floatvalue) voidwriteInt(intvalue) voidwriteLong(longvalue) finalvoidwriteObject(Objectobject) voidwriteShort(intvalue) voidwriteUTF(Stringvalue) voidwriteUnshared(Objectobject) ObjectInputStream函数列表 //构造函数 ObjectInputStream(InputStreaminput) intavailable() voidclose() voiddefaultReadObject() intread(byte[]buffer,intoffset,intlength) intread() booleanreadBoolean() bytereadByte() charreadChar() doublereadDouble() ObjectInputStream.GetFieldreadFields() floatreadFloat() voidreadFully(byte[]dst) voidreadFully(byte[]dst,intoffset,intbyteCount) intreadInt() StringreadLine() longreadLong() finalObjectreadObject() shortreadShort() StringreadUTF() ObjectreadUnshared() intreadUnsignedByte() intreadUnsignedShort() synchronizedvoidregisterValidation(ObjectInputValidationobject,intpriority) intskipBytes(intlength)
演示程序
源码如下(ObjectStreamTest.java):
/** *ObjectInputStream和ObjectOutputStream测试程序 * *注意:通过ObjectInputStream,ObjectOutputStream操作的对象,必须是实现了Serializable或Externalizable序列化接口的类的实例。 * * */ importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; importjava.io.Serializable; importjava.util.Map; importjava.util.HashMap; importjava.util.Iterator; publicclassObjectStreamTest{ privatestaticfinalStringTMP_FILE="box.tmp"; publicstaticvoidmain(String[]args){ testWrite(); testRead(); } /** *ObjectOutputStream测试函数 */ privatestaticvoidtestWrite(){ try{ ObjectOutputStreamout=newObjectOutputStream( newFileOutputStream(TMP_FILE)); out.writeBoolean(true); out.writeByte((byte)65); out.writeChar('a'); out.writeInt(20131015); out.writeFloat(3.14F); out.writeDouble(1.414D); //写入HashMap对象 HashMapmap=newHashMap(); map.put("one","red"); map.put("two","green"); map.put("three","blue"); out.writeObject(map); //写入自定义的Box对象,Box实现了Serializable接口 Boxbox=newBox("desk",80,48); out.writeObject(box); out.close(); }catch(Exceptionex){ ex.printStackTrace(); } } /** *ObjectInputStream测试函数 */ privatestaticvoidtestRead(){ try{ ObjectInputStreamin=newObjectInputStream( newFileInputStream(TMP_FILE)); System.out.printf("boolean:%b\n",in.readBoolean()); System.out.printf("byte:%d\n",(in.readByte()&0xff)); System.out.printf("char:%c\n",in.readChar()); System.out.printf("int:%d\n",in.readInt()); System.out.printf("float:%f\n",in.readFloat()); System.out.printf("double:%f\n",in.readDouble()); //读取HashMap对象 HashMapmap=(HashMap)in.readObject(); Iteratoriter=map.entrySet().iterator(); while(iter.hasNext()){ Map.Entryentry=(Map.Entry)iter.next(); System.out.printf("%-6s--%s\n",entry.getKey(),entry.getValue()); } //读取Box对象,Box实现了Serializable接口 Boxbox=(Box)in.readObject(); System.out.println("box:"+box); in.close(); }catch(Exceptione){ e.printStackTrace(); } } } classBoximplementsSerializable{ privateintwidth; privateintheight; privateStringname; publicBox(Stringname,intwidth,intheight){ this.name=name; this.width=width; this.height=height; } @Override publicStringtoString(){ return"["+name+":("+width+","+height+")]"; } }
运行结果:
boolean:true byte:65 char:a int:20131015 float:3.140000 double:1.414000 two--green one--red three--blue box:[desk:(80,48)]
以上所述是小编给大家介绍的ObjectInputStream和ObjectOutputStream的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!