会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132390个问题
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 10621楼

public void start() {
    Menu menu = new Menu();
    TelNoteRegex regex = new TelNoteRegex();
    Operate operate = new Operate();
    File file = new File("/Users/JAVA/a.txt");
    ObjectInputStream ois = null;
    Object o = null;
    try {
        file.createNewFile();
        ois = new ObjectInputStream(new FileInputStream(file));
        if ((o = ois.readObject())==null){
            return;
        }else {
            operate.ObjectInputStreamDemo();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    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:
                operate.ObjectOutputStreamDemo();
                System.exit(0);

        }
    }
}

我在这里做了判断了,为什么还会报EOF?

public void ObjectOutputStreamDemo(){
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("/Users/JAVA/a.txt",true));
        for (Person temp:list){
            oos.writeObject(temp);
        }
        oos.flush();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if (oos!=null){
                oos.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
public void ObjectInputStreamDemo(){
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream("/Users/JAVA/a.txt"));
        Person a = null;
        while(true){
            if ((a = (Person) ois.readObject())==null){
                break;
            }
            Person person =(Person) ois.readObject();
            this.list.add(person);
        }

    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if(ois!=null){
                ois.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

StreamCorruptedException这个报错我该怎么修改呢,有时候if语句中还会报类型强制转换的错误是为什么呢

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 10622楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 10624楼
JAVA 全系列/第四阶段:网页编程和设计/HTML5(旧) 10625楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 10626楼
JAVA 全系列/第四阶段:网页编程和设计/HTML5(旧) 10627楼
WEB前端全系列/第九阶段:HTML5新特性模块/(旧)H5新特性 10628楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 10629楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 10630楼
Java架构课程/第十六阶段:高并发与负载均衡/大型网站高并发处理 10631楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 10633楼
Python 全系列/第十一阶段:重量级Web框架-Django/Redis的入门与应用(拓展) 10634楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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