java io系列05之 ObjectInputStream 和 ObjectOutputStream

来源:互联网 发布:linux vi esc 没反应 编辑:程序博客网 时间:2024/06/05 08:41

本文摘抄至:skywang12345

摘要:介绍了ObjectInputStream 和 ObjectOutputStream的使用。它可以对基本数据实现类Serializable或Externalizable序列化接口的类的实例进行操作支持。

本章,我们学习ObjectInputStream 和 ObjectOutputStream

ObjectInputStream 和 ObjectOutputStream 介绍


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

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

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序列化接口的类的实例。 * @author hu */public class ObjectStreamTest {    private static final String TMP_FILE = "box.tmp";    public static void main(String[] args) {        testWrite();        testRead();     }    /**ObjectOutoutStream 测试函数*/    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(20161002);            out.writeFloat(3.14f);            out.writeDouble(1.1414d);            //写入 HasMap 对象            HashMap map = new HashMap<>();            map.put("one", "red");            map.put("two", "green");            map.put("three", "blue");            out.writeObject(map);            Box box = new Box("desk", 80, 48);            out.writeObject(box);            out.close();        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * ObjectInputStream 测试函数     */    private static void testRead() {        try {            ObjectInputStream in = new ObjectInputStream(                    new FileInputStream(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对象            HashMap map = (HashMap) in.readObject();            Iterator iter = map.entrySet().iterator();            while (iter.hasNext()) {                Map.Entry entry = (Map.Entry)iter.next();                System.out.printf("%-6s -- %s\n" , 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 String name;    private int width;       private int height;     public Box(String name,int width, int height) {        super();        this.width = width;        this.height = height;        this.name = name;    }    @Override    public String toString() {        return "Box [width=" + width + ", height=" + height + ", name=" + name + "]";    }  }

运行结果:

boolean:true
byte:65
char:a
int:20161002
float:3.140000
double:1.141400
one – red
two – green
three – blue
box: Box [width=80, height=48, name=desk]

0 0
原创粉丝点击