会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132474个问题
Python 全系列/第一阶段:Python入门/序列 15316楼
WEB前端全系列/第十四阶段:微信小程序/实战_百战商城 15317楼

老师,电话本项目作业,我在operate类的addOpration中添加了ObjectOutputStream,

public void addOperation() {
    TelNoteRegex telNoteRegex=new TelNoteRegex();
    String name=telNoteRegex.nameValidate();
    String age=telNoteRegex.ageValidate();
    String sex=telNoteRegex.sexValidate();
    String telNum=telNoteRegex.telNumValidate();
    String address=telNoteRegex.addressValidate();
    Person person=new Person(name,age,sex,telNum,address);
    this.list.add(person);
    person.setId(this.list.size());
    ObjectOutputStream oos=null;
    try {
        oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/person.txt",true)));
        oos.writeObject(person);
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if (oos!= null) {
                oos.close();
            }
        }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

然后在Operate类创建了一个ObjectInputStream方法,

/**
 * 对象输入流
 * 初始化list
 */
public void ObjectInputStream(){
    ObjectInputStream ois=null;
    try{
        ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("d:/person.txt")));
        List list=(List<Person>)ois.readObject();
        this.list=list;
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try{
            if (ois!=null){
                ois.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

再在app类的start方法里调用operate的ObjectInputStream方法,初始化list内的信息,以便下次打开程序时读取之前存储的信息。

public void start(){
    Menu menu=new Menu();
    TelNoteRegex regex=new TelNoteRegex();
    Operate operate=new Operate();
    operate.ObjectInputStream();//初始化Operate中的list
    while (true) {
        menu.mainMenu();
        int item=regex.menuItemValidate(1,6);
        switch (item){
            case 1:operate.addLogic();break;
            case 2:operate.searchLogic();break;
            case 3:operate.modifyLogic();break;
            case 4:operate.deleteLogic();break;
            case 5:operate.orderLogic();break;
            case 6:System.exit(0);  //结束虚拟机

程序运行后有如下报错。

java.io.EOFException
	at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2638)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3113)
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:853)
	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
	at com.wxc.Operate.ObjectInputStream(Operate.java:159)
	at com.wxc.App.start(App.java:28)
	at com.wxc.App.main(App.java:18)

不知道这个思路问题出在哪里。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 15320楼
Python 全系列/第二阶段:Python 深入与提高/模块 15323楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 15326楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 15328楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 15330楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637