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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算