提示异常,怎么改呢?
package com.imooc.serial; import java.io.Serializable; public class Goods implements Serializable { private String num; private String name; private double price; public Goods(String num, String name, double price) { super(); this.num = num; this.name = name; this.price = price; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "商品信息 [编号=" + num + ", 名字=" + name + ", 价格=" + price + "]"; } } package com.imooc.serial; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashSet; public class GoodsTest { public static void main(String[] args) { Goods goods1=new Goods("gd01","电脑",3000); Goods goods2=new Goods("gb02","手机",1000); Goods goods3=new Goods("gb03","手表",4000); HashSet<Goods> set=new HashSet<Goods>(); set.add(goods1); set.add(goods2); set.add(goods3); try { //序列化,将Java对象转换为字节序列的过程,即写 FileOutputStream fos=new FileOutputStream("imooc.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); //反序列化:将字节序列恢复为Java对象的过程,即读 FileInputStream fis=new FileInputStream("imooc.txt"); ObjectInputStream ois=new ObjectInputStream(fis); //对象信息写入 //oos.writeObject(goods1); oos.writeObject(set); //oos.writeBoolean(true); oos.flush(); //对象信息读出 try { HashSet<Goods> set2=new HashSet(); set2.add((Goods)ois.readObject()); for(Goods g:set2) { System.out.println(g); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(ois.readBoolean()); fos.close(); oos.close(); fis.close(); ois.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } }
0
收起
正在回答 回答被采纳积分+1
1回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星