zoukankan      html  css  js  c++  java
  • ObjectInputStream和ObejctOutputStream

    ObjectInputStream和ObejctOutputStream的作用是:对基本数据和对象进行序列化支持.

    ObjectOutputStream提供对"基本数据对象"的持久存储.ObjectInputStream提供对"基本数据对象"的读取.

    public class ObjectStreamTest {
        private static final String PATH = "D:\baiduyun\filetest\rrr.txt";
    
        public static void main(String[] args) throws Exception {
        testWrite();
        testRead();
        }
    
        public static void testWrite() throws FileNotFoundException, IOException {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(PATH)));
        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();
    
        }
    
        public static void testRead() throws FileNotFoundException, IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(PATH));
        System.out.printf("boolean:%b
    ", in.readBoolean());
        System.out.printf("byte:%d
    ", (in.readByte() & 0xff));
        System.out.printf("char:%c
    ", in.readChar());
        System.out.printf("int:%d
    ", in.readInt());
        System.out.printf("float:%f
    ", in.readFloat());
        System.out.printf("double:%f
    ", 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
    ", entry.getKey(), entry.getValue());
        }
        // 读取Box对象,Box实现了Serializable接口
        Box box = (Box) in.readObject();
        System.out.println("box: " + box);
    
        in.close();
    
        }
    
        static 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 + ") ]";
        }
        }
    }
  • 相关阅读:
    LPC2138微控制器之定时器、看门狗、VIC实例
    Expression表达式目录树动态拼接 反射获取泛型方法
    泛型委托 Func<out T>,当返回的数据是一个匿名类型的时候该怎么办
    git stash与git commit的区别
    原码,补码,反码 和 有符合,无符号 整数知识总结
    二进制的 按位与、按位或、按位异、按位取反 的简单总结
    Asp.net MVC 中的TempData对象的剖析
    对Cookie和Session的理解
    MVC中IActionFilter过滤器俄罗斯套娃的实现方式
    分布式缓存
  • 原文地址:https://www.cnblogs.com/zhangj-ymm/p/9861360.html
Copyright © 2011-2022 走看看