鱼C论坛

 找回密码
 立即注册
查看: 2209|回复: 1

JAVA------IO流中对象流的读取问题

[复制链接]
发表于 2019-4-22 16:55:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我在程序的开始先读取文件里的对象流,发现这样会使我的文件内容清空,求解答


  1. import java.io.Serializable;

  2. class Student implements Serializable{//学生类

  3.         private static final long serialVersionUID = 1L;
  4.         String name="初始化";
  5.         String address="初始化";
  6.         char sex='男';
  7.         int age=18;
  8.         float mathScore=100;//数学成绩
  9.         float englishScore=100;//英语成绩

  10.         void setName(String name) {
  11.                 this.name=name;
  12.         }
  13.         void setAddress(String address) {
  14.                 this.address=address;
  15.         }
  16.         void setSex(char sex) {
  17.                 this.sex=sex;
  18.         }
  19.         void setAge(int age) {
  20.                 this.age=age;
  21.         }
  22.         void setMathScore(float mathScore) {
  23.                 this.mathScore = mathScore;
  24.         }
  25.         void setEnglishScore(float englishScore) {
  26.                 this.englishScore = englishScore;
  27.         }
  28.         
  29.         String getName() {
  30.                 return this.name;
  31.         }
  32.         String getAdress() {
  33.                 return this.address;
  34.         }
  35.         char getSex() {
  36.                 return this.sex;
  37.         }
  38.         int getAge() {
  39.                 return this.age;
  40.         }
  41.         float getMathScore() {
  42.                 return this.mathScore;
  43.         }
  44.         float getEnglishScore() {
  45.                 return this.englishScore;
  46.         }
  47. }
复制代码



  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. import java.util.concurrent.TimeUnit;

  9. public class Main {
  10.         public static void main(String[] args) throws IOException, ClassNotFoundException {

  11.                 ArrayList<Student> list = new ArrayList<>();

  12. //                File student = new File("E:\\File类", "student.txt");
  13.                 FileOutputStream outstu = new FileOutputStream("E:\\File类\\\\student.txt");
  14.                 ObjectOutputStream out = new ObjectOutputStream(outstu);// 对象流输出到文件
  15.                 FileInputStream instu = new FileInputStream("E:\\File类\\\\student.txt");
  16.                 ObjectInputStream in = new ObjectInputStream(instu);// 对象流输入到程序

  17.                 ArrayList<Student> liststu = new ArrayList<>();
  18.                 try {
  19.                         @SuppressWarnings("unchecked")
  20.                         ArrayList<Student> liststu1 = (ArrayList<Student>) in.readObject();// 读取文件内的数组 存到liststu内
  21.                         liststu = liststu1;
  22.                         System.out.println("文件内原有学生");
  23.                         for (int i = 0; i < liststu.size(); i++) {//数组文件内数组含有的学生
  24.                                 System.out.println(liststu.get(i).name + " " + liststu.get(i).address + " " + liststu.get(i).sex + " "
  25.                                                 + liststu.get(i).age + " " + liststu.get(i).mathScore + " " + liststu.get(i).englishScore);
  26.                         }
  27.                 } catch (Exception e) {
  28.                         System.out.println(e);
  29.                 }
  30.                
  31. //                try {
  32. //                TimeUnit.SECONDS.sleep(1);//让程序等待1秒
  33. //        } catch (InterruptedException e1) {
  34. //                e1.printStackTrace();
  35. //        }

  36.                 System.out.println("录入学生信息:");
  37.                 Scanner sc = new Scanner(System.in);
  38.                 for (;;) {// 录入学生
  39.                         System.out.println("依次输入姓名、地址、性别、年龄、数学成绩和英语成绩:");
  40.                         Student stu = new Student();
  41.                         String name = sc.next();
  42.                         if (name.charAt(0) == '0') {// 结束录入
  43.                                 break;
  44.                         }
  45.                         stu.setName(name);
  46.                         stu.setAddress(sc.next());
  47.                         stu.setSex(sc.next().charAt(0));
  48.                         stu.setAge(sc.nextInt());
  49.                         stu.setMathScore(sc.nextFloat());
  50.                         stu.setEnglishScore(sc.nextFloat());
  51.                         list.add(stu);// 添加到list数组
  52.                 }

  53.                 for (int i = 0; i < list.size(); i++) {// 更新名字和地址相同的学生信息
  54.                         for (int j = 0; j < liststu.size(); j++) {
  55.                                 if (list.get(i).name == liststu.get(j).name && list.get(i).address == liststu.get(j).address) {
  56.                                         liststu.get(j).address = list.get(i).address;
  57.                                         liststu.get(j).sex = list.get(i).sex;
  58.                                         liststu.get(j).age = list.get(i).age;
  59.                                         liststu.get(j).mathScore = list.get(i).mathScore;
  60.                                         liststu.get(j).englishScore = list.get(i).englishScore;
  61.                                         break;
  62.                                 }
  63.                         }
  64.                         liststu.add(list.get(i));// 直接添加到尾部
  65.                 }

  66. //                for (int i = 0; i < liststu.size(); i++) {
  67. //                        out.writeObject(liststu.get(i));// 把录入的跟原有的学生一起写进文件  (BUT只能把最后一个学生写入文件)
  68. //                }               

  69.                 out.writeObject(liststu);// 把新录入的跟原有的学生 数组 写进文件

  70.                 try {
  71.                         System.out.println("文件内当前学生");
  72.                         while (true) { // 输出文件里数组内的所有学生对象
  73.                                 @SuppressWarnings("unchecked")
  74.                                 ArrayList<Student> stu = (ArrayList<Student>) in.readObject();
  75.                                 for (int i = 0; i < stu.size(); i++) {
  76.                                         System.out.println(stu.get(i).name + " " + stu.get(i).address + " " + stu.get(i).sex + " "
  77.                                                         + stu.get(i).age + " " + stu.get(i).mathScore + " " + stu.get(i).englishScore);
  78.                                 }
  79.                         }
  80.                 } catch (Exception e) {
  81.                 }

  82.                 in.close();
  83.                 out.close();
  84.                 sc.close();
  85.         }
  86. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-4 11:18:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 04:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表