java io ----- 05 (ObjectInputStream 和ObjectOutputStream )

ObjectInputStream和ObjectOutputStream

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

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

注意,只有支持java.io.Seralizable 或 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)

演示程序

package com.tuhu.filt.javadata;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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);
            //写入HshMap对象
            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 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 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 "Box{" +
                "width=" + width +
                ", height=" + height +
                ", name='" + name + '\'' +
                '}';
    }
}

结果如图所示

boolean:true
byte:65
char:a
int:20131015
float:3.140000
double:1.414000
one    -- red
two    -- green
three  -- blue
box: Box{width=80, height=48, name='desk'}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值