微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ObjectInputStream 和 ObjectOutputStream 介绍_动力节点Java学院整理

ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持。本文给大家详细介绍了ObjectInputStream 和 ObjectOutputStream的相关知识,感兴趣的朋友一起学习吧

ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持

创建“文件输出流”对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对“基本数据或对象”的持久存储;当我们需要读取这些存储的“基本数据或对象”时,可以创建“文件输入流”对应的ObjectInputStream,进而读取出这些“基本数据或对象”。

注意: 只有支持 java.io.Serializable 或 java.io.Externalizable 接口的对象才能被ObjectInputStream/ObjectOutputStream所操作!

ObjectOutputStream 函数列表 

// 构造函数 ObjectOutputStream(OutputStream output) // public函数 void close() void defaultWriteObject() void flush() ObjectOutputStream.PutField putFields() void reset() void useProtocolVersion(int version) void write(int value) void write(byte[] buffer, int offset, int length) void writeBoolean(boolean value) void writeByte(int value) void writeBytes(String value) void writeChar(int value) void writeChars(String value) void writeDouble(double value) void writeFields() void writeFloat(float value) void writeInt(int value) void writeLong(long value) final void writeObject(Object object) void writeShort(int value) void writeUTF(String value) void writeUnshared(Object object) ObjectInputStream 函数列表 // 构造函数 ObjectInputStream(InputStream input) int available() void close() void defaultReadobject() int read(byte[] buffer, int offset, int length) int read() boolean readBoolean() byte readByte() char readChar() double readDouble() ObjectInputStream.GetField readFields() float readFloat() void readFully(byte[] dst) void readFully(byte[] dst, int offset, int byteCount) int readInt() String readLine() long readLong() final Object readobject() short readShort() String readUTF() Object readUnshared() int readUnsignedByte() int readUnsignedShort() synchronized void registerValidation(ObjectInputValidation object, int priority) int skipBytes(int length)

演示程序

源码如下(ObjectStreamTest.java):

/** * ObjectInputStream 和 ObjectOutputStream 测试程序 * * 注意:通过ObjectInputStream, ObjectOutputStream操作的对象,必须是实现了Serializable或Externalizable序列化接口的类的实例。 * * */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Map; import java.util.HashMap; import java.util.Iterator; public class ObjectStreamTest { private static final String TMP_FILE = "Box.tmp"; public static void main(String[] args) { testWrite(); testRead(); } /** * ObjectOutputStream 测试函数 */ private static void testWrite() { try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(TMP_FILE)); out.writeBoolean(true); out.writeByte((byte)65); out.writeChar('a'); out.writeInt(20131015); out.writeFloat(3.14F); out.writeDouble(1.414D); // 写入HashMap对象 HashMap map = new HashMap(); map.put("one", "red"); map.put("two", "green"); map.put("three", "blue"); out.writeObject(map); // 写入自定义Box对象,Box实现了Serializable接口 Box Box = new Box("desk", 80, 48); out.writeObject(Box); out.close(); } catch (Exception ex) { ex.printstacktrace(); } } /** * ObjectInputStream 测试函数 */ private static void testRead() { try { ObjectInputStream in = new ObjectInputStream( new FileInputStream(TMP_FILE)); System.out.printf("boolean:%bn" , in.readBoolean()); System.out.printf("byte:%dn" , (in.readByte()&0xff)); System.out.printf("char:%cn" , in.readChar()); System.out.printf("int:%dn" , in.readInt()); System.out.printf("float:%fn" , in.readFloat()); System.out.printf("double:%fn" , in.readDouble()); // 读取HashMap对象 HashMap map = (HashMap) in.readobject(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); System.out.printf("%-6s -- %sn" , entry.getKey(), entry.getValue()); } // 读取Box对象,Box实现了Serializable接口 Box Box = (Box) in.readobject(); System.out.println("Box: " + Box); in.close(); } catch (Exception e) { e.printstacktrace(); } } } class Box implements Serializable { private int width; private int height; private String name; public Box(String name, int width, int height) { this.name = name; this.width = width; this.height = height; } @Override public String toString() { 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 的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程之家网站的支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐