码迷,mamicode.com
首页 > 其他好文 > 详细

ObjectOutputStream

时间:2015-07-17 20:17:26      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

public class Test {

    public static void main(String[] args) throws Exception {

         //writeObject();
        readObject();
    }

    private static void readObject() throws Exception {
        FileInputStream fis = new FileInputStream("F:/abe.data");
        ObjectInputStream ois = new ObjectInputStream(fis);
        User user = null;
        for (int i = 0; i < 200; i++) {
            user = (User) ois.readObject();
            System.out.println(user);
        }
        ois.close();
        fis.close();
    }

    private static void writeObject() throws Exception {
        File file = new File("F:/abe.data");
        FileOutputStream fos = new FileOutputStream(file, true);
        ObjectOutputStream oos = null;
        if (file.length() < 1) {
            oos = new ObjectOutputStream(fos);
        } else {
            oos = new CustomObjectOutputStream(fos);
        }
        for (int i = 0; i < 100; i++) {
            User user = new User();
            user.setId(i);
            user.setUsername("abc" + i);
            user.setPassword("efg" + i);
            oos.writeObject(user);
        }
        oos.close();
        fos.close();
    }

}

 

public class CustomObjectOutputStream extends ObjectOutputStream {
    public CustomObjectOutputStream() throws IOException {
        super();
    }

    public CustomObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    @Override
    protected void writeStreamHeader() throws IOException {
        return;
    }
}

 

ObjectOutputStream

标签:

原文地址:http://www.cnblogs.com/dingyingsi/p/4655227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!