zhjhcjl9294 2018-10-02 07:26 采纳率: 0%
浏览 2544

序列化可以正常存入多个对象,反序列化只能读入一个对象,求助求助

public class StuOpration {
static File file = new File("student.txt");
public void writeStreamheader(){}
//添加一个学生
public static void get() throws FileNotFoundException{
FileOutputStream os = new FileOutputStream(file,true);
}
public static void add(Student stu) throws Exception{
if(file.length() ==0){
get();
}
FileOutputStream os = new FileOutputStream(file,true);
ObjectOutputStream oos = new ObjectOutputStream(os );
oos.writeObject(stu);
oos.writeChars("\n");
oos.close();
System.out.println("添加成功");
}
//修改学生信心
public void update(int num,int age) throws Exception{
FileInputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream( is);
Student stu = (Student)ois.readObject();
while(is.available()>0){
if(stu.getNum() == num){
stu.setAge(age);
System.out.println("修改成功");
return;
}
stu = (Student)ois.readObject();
}
ois.close();
}
//删除一个学生
public void delete(int num) throws Exception{
FileInputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream( is);
Student stu = (Student)ois.readObject();
while(is.available()>0){
System.out.println(stu);
stu = (Student)ois.readObject();
}
ois.close();
}

public void show() throws Exception{
     FileInputStream is = new FileInputStream(file);
    ObjectInputStream ois = new ObjectInputStream( is);
    Student  obj=(Student)ois.readObject();
    while(true){
        try {
            System.out.println(obj);
            obj=(Student)ois.readObject();

        } catch (Exception e) {
            System.out.println("数据已经读取完毕");
            break;
        }
    }
    ois.close();
}

}

请输入对应的编号:
1: 添加学生 2: 修改学生信息 3:删除学生4:显示学生信息
4
Student [name=张三, password=123, score=45.0, num=1, age=23]
数据已经读取完毕





  • 写回答

5条回答

  • lixiaozhen007 2018-10-02 14:24
    关注

    你的实现序列化接口呢,不是说数据流就是序列化啊

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog