日期:2014-05-20  浏览次数:20730 次

java 类型转换,一个深度的问题?
我将数据从数据库中读取出来放到List中,然后转化为byte存储到缓冲区中!我读的时候,怎么再让他转换为List???

存储前List数据格式跟读取后的ByteBuffer存储的数据格式一样,如下:

[com.hg.sw.entity.SwControlparam@19bc716, com.hg.sw.entity.SwControlparam@1d3ac6e, com.hg.sw.entity.SwControlparam@1dd8664]


------解决方案--------------------
。。。。。。
明显要用序列化嘛
前提是你List里面的对象也要实现序列化,不然弄出来就是空的
写:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Output.dat"));
oos.writeObject(list);

读:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Output.data"));
List<?> list = (List<?>) ois.readObject();

大致是这样,我手打的,可能有误哈。

------解决方案--------------------
序列化 反序列化
------解决方案--------------------
Java code


try {

            FileOutputStream fos = new FileOutputStream("test.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Student1 s1 = new Student1("张三", "12345");
            Student1 s2 = new Student1("王五", "54321");

            oos.writeObject(s1);
            oos.writeObject(s2);

            oos.close();

            FileInputStream fis = new FileInputStream("test.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);

            Student1 s3 = (Student1) ois.readObject();
            Student1 s4 = (Student1) ois.readObject();

            System.out.println(s3);
            System.out.println(s4);

            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }