会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132454个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1591楼

老师,电话本项目作业,我在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 流技术(旧) 1594楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1597楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 1598楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1601楼

老师,文件字节和字符输出流,都是当缓存区满了之后,才会自动调用flush()进行刷新

我测试了一下字符和字节不关闭,IO流是否能将数据写入文件中

字节不关闭IO流可以,字符不行,必须关闭IO或使用flush()刷新一下

public class Example{
    public static void main(String[] args) throws IOException{
        FileWriter fil = new FileWriter("D:/a.txt");
        char[] arry = new char[] {'s','s'};
        fil.write(arry);
//        fil.close();
    }
}
public class TestFileOutputStream {
    public static void main(String[] args) throws IOException {
        //搭建通道(程序与目的地)
        FileOutputStream fil = new FileOutputStream("D:/keobei2.txt");
        //write()     一次写入一个字节
        //write(byte[] buf)    //将数组中所有内存写入缓存区中
        //write(byte[] buf,int off,int len)    //指定一次写入多少个字节
        //write(String str) //直接写入字符串
        //flush() 强制刷新缓存区
        //close() 关闭IO流
        //注意:写入的数据不是直接写入文件当中,而是写入缓存区当中
        //当缓冲满了后自动调用flush()方法,刷新缓存区,将缓存区的
        //内容存到文件当中
        
        //一次写入一个字节,以数字形式写入
        //fil.write(97);
        //一次写入一个字符串
        //但是他不能直接字符串,需要转换一次
        //fil.write(String.valueOf("你好呀").getBytes());
        //一次输入多个字节
        byte[] arry = "你好我是你的老师1".getBytes();
//        byte[] arry02 = new byte[] {02,52,35,15,124};
        fil.write(arry);
        //关闭IO流
//        if (fil != null) {
//            fil.close();
//        }
    }


JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1602楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1603楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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