Object流的一般用法


import java.io.*;

/**
 * Created by wang on 2017/7/12.
 */
public class TestObject {
}

class Person implements Serializable {
    private String name;
    private int age;
    private String race;
    private String location;

    public Person(String name, int age, String race, String location) {
        this.name = name;
        this.age = age;
        this.race = race;
        this.location = location;
    }

    @Override
    public String toString() {
        return name+" "+ age + " " + race + " " + location;
    }
}

class Test {
    public static void main(String[] args) {
        Person aPerson = new Person("wzong",28,"yellow","许以");
        System.out.println(aPerson);

        try {
            ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream("d:/dayuan/day24/object.dat"));
            out.writeObject(aPerson);
            out.close();

            ObjectInputStream in = new ObjectInputStream(
                    new FileInputStream("d:/dayuan/day24/object.dat"));

            Person bPerson = (Person) (in.readObject());
            out.close();
            System.out.println(bPerson);
            System.out.println(aPerson == bPerson);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

1    需要实现Serializable接口;

2    有读入读出的标准格式;

3    数据储存在dat文件中;

4    读入读出的对象数据是连个对象;

5    需要处理ClassNotFoundException异常;

转载于:https://my.oschina.net/u/3544275/blog/1358698

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值