java 类型转换,一个深度的问题?

gzqGyula 2012-07-31 10:33:03
我将数据从数据库中读取出来放到List中,然后转化为byte存储到缓冲区中!我读的时候,怎么再让他转换为List???

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

[com.hg.sw.entity.SwControlparam@19bc716, com.hg.sw.entity.SwControlparam@1d3ac6e, com.hg.sw.entity.SwControlparam@1dd8664]
...全文
125 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
五哥 2012-07-31
  • 打赏
  • 举报
回复


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();
}
五哥 2012-07-31
  • 打赏
  • 举报
回复
序列化 反序列化
JiinYuu 2012-07-31
  • 打赏
  • 举报
回复
。。。。。。
明显要用序列化嘛
前提是你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();

大致是这样,我手打的,可能有误哈。
gzqGyula 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你的转换过程呢?反着转回来就OK了嘛
[/Quote]
FileChannel fc = new FileOutputStream("Output.txt").getChannel();
fc.write(ByteBuffer.wrap(dataList.toString().getBytes("UTF-16BE")));
你看怎么转换?
龙四 2012-07-31
  • 打赏
  • 举报
回复
你的转换过程呢?反着转回来就OK了嘛
gzqGyula 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
Java code



try {

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

Student1 s1 = new Stude……
[/Quote]

谢谢,终于解决了!!
gzqGyula 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
。。。。。。
明显要用序列化嘛
前提是你List里面的对象也要实现序列化,不然弄出来就是空的
写:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Output.dat"));
oos.writeObject(list);

读:
ObjectInputStream ois = new Ob……
[/Quote]
太谢谢了,是这样的!!

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧