//有好心人帮忙指点么Java,第一回发帖子。package com.test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class StudentManager { //可以再用对象流升级 ObjectInputStream 
public static void main(String[] args) throws Exception {
String fileName = "Student.txt";
// 为了能实现返回主界面的操作while(true)
// 这是学生管理系统的主界面
// while (true) { System.out.println("--------欢迎来到学生管理系统---------");
System.out.println("1查看所有学生");
System.out.println("2添加学生");
System.out.println("3删除学生");
System.out.println("4修改学生");
System.out.println("5退出");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String choiceString = sc.next();
// 用switch实现选择
switch (choiceString) {
case "1":
// 查看所有学生
findAllStudent(fileName);
StudentManager.main(null);
break;
case "2":
// 添加学生
addStudent(fileName);
StudentManager.main(null);
break;
case "3":
// 删除学生
deleteStudent(fileName);
StudentManager.main(null);
break;
case "4":
// 修改学生
updateStudent(fileName);
StudentManager.main(null);
break;
case "5":
// 退出a
// System.out.println("谢谢你的使用");
// break;
default:
System.out.println("谢谢你的使用");
System.exit(0);
break;
}
}
// } // 从文件中读数据到集合
@SuppressWarnings("unchecked")
public static void readDate(String fileName, ArrayList<Student> array) throws Exception {
/* BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
Student s = new Student();
s.setId(arr[0]);
s.setName(arr[1]);
s.setAge(arr[2]);
s.setAddress(arr[3]);
array.add(s);
}
br.close();*/

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
if(new File("Student.txt").length() != 0) {
Object obj = ois.readObject();
if(obj instanceof ArrayList)
// array = (ArrayList)obj;  这样只是交接地址值 除了方法地址值就没了,集合还是为空
array.addAll((ArrayList)obj);   //复制集合中的内容
}
ois.close();
} // 把集合中的内容写到文件中
public static void writeDate(String fileName, ArrayList<Student> array) throws Exception {
/* BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
StringBuilder sb = new StringBuilder();
sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",")
.append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
}
bw.close();*/

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(array);
oos.close();
} // 查看所有学生
public static void findAllStudent(String fileName) throws Exception {
// 创建集合
ArrayList<Student> array = new ArrayList<>();
// 把文件中的数据放到集合中;
readDate(fileName, array);
// 首先判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续向下执行
if (array.size() == 0) { // 或者用array.isEmpty();
System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择您的操作");
return;
}
System.out.println("学号\t姓名\t年龄\t居住地");// 提示
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
} // 添加学生
public static void addStudent(String fileName) throws Exception {
// 创建集合
ArrayList<Student> array = new ArrayList<>();
// 把文件中的数据放到集合中;
readDate(fileName, array);
Scanner sc = new Scanner(System.in); // 为了让id能够被访问到,我们把id定义在了循环的外面
String id; // 为了让代码能够回到这里,用循环
while (true) {
System.out.println("请输入学生学号:");
id = sc.next();
// 学号不能有重复
// 定义一个标记
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getId().equals(id)) {
flag = true;// 说明学号被占用了
break;
}
} if (flag) {
System.out.println("你输入的学号已经被占用,请重新输入");
} else {
break;
}
} System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
String age = sc.next();
System.out.println("请输入学生居住地:");
String address = sc.next();
// 创建学生对象
Student s = new Student(id, name, age, address);
// 把学生对象作为元素添加到集合
array.add(s);
// 把添加的信息要写到文件中
writeDate(fileName, array);
// 给出提示
System.out.println("添加学生成功");
}
// 删除学生 // 删除学生
public static void deleteStudent(String fileName) throws Exception {
// 创建集合
ArrayList<Student> array = new ArrayList<>();
// 把文件中的数据放到集合中;
readDate(fileName, array);
// 删除学生的思路: 键盘输入一个学号,到集合中去查找,看是否有学生使用该学号,如果有就删除该学生
Scanner sc = new Scanner(System.in);
System.out.println("请输入您要删除的学生学号:");
String id = sc.next(); // 定义一个索引
int index = -1; // 遍历集合
for (int i = 0; i < array.size(); i++) {
// 获取每一个学生对象
Student s = array.get(i);
// 那这个学生的学号和键盘录入的学号进行比较
if (s.getId().equals(id)) {
index = i;
// array.remove(i);//根据索引删除
break;
}
}
// 输出提示
if (index == -1) {
System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请重新你的选择");
} else {
array.remove(index);
// 把添加的信息要写到文件中
writeDate(fileName, array);
System.out.println("删除学成功");
}
}
// 修改学生 // 修改学生
public static void updateStudent(String fileName) throws Exception {
// 创建集合
ArrayList<Student> array = new ArrayList<>();
// 把文件中的数据放到集合中;
readDate(fileName, array);
// 修改学生思路: 键盘录入一个学号,到集合中去查找看是否有学生使用该学号,如果有就修改该学生: Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:");
String id = sc.next(); // 定义一个索引
int index = -1; // 遍历集合
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("不好意思,不好意思,你要修改的学号对应的学生信息不存在,请重新你的选择");
} else {
System.out.println("请输入新的学生姓名:");
String name = sc.next();
System.out.println("请输入新的学生年龄:");
String age = sc.next();
System.out.println("请输入新的学生居住地:");
String address = sc.next();
// 创建学生对象
Student s = new Student(id, name, age, address);
// 修改集合中的学生对象
array.set(index, s);
// 把添加的信息要写到文件中
writeDate(fileName, array);
// 给出提示
System.out.println("修改学生成功");
}
}
}//这是报错
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 37313633
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at com.test.StudentManager.readDate(StudentManager.java:79)
at com.test.StudentManager.updateStudent(StudentManager.java:217)
at com.test.StudentManager.main(StudentManager.java:48)

解决方案 »

  1.   

    1,你既然写入对象为student 为什么读入对象不用student?2.    array.addAll((ArrayList) obj); // 复制集合中的内容  addAll后面参数为list<object>,你加入的只是一个object类型
      

  2.   

    写入的时候oos.writeObject(array);array是一个 ArrayList<Student> list,
    你读入的时候Object obj = ois.readObject();没指名Object格式,程序不知道怎么读入。输入输出要对应。不然无法识别。
      

  3.   

    readObject,writeObject的对象必须序列化的实例。不然,无法读取。
    public static List<Student> readDate(String fileName)throws Exception {
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fileName));
    List<Student> array=new ArrayList<Student>();
    Student stu;
    while((stu=(Student)ois.readObject())!=null) {
    array.add(stu);
    }
    ois.close();
    return array;
    }

    public static void writeDate(String fileName,List<Student> array)throws Exception{
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fileName));
    array.forEach(i->{
    try {
    oos.writeObject(i);
    oos.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    });
    }
    public class Student implements Serializable {
    private int id;
    private String name;
    public Student(int id, String name) {
    super();
    this.id = id;
    this.name = name;
    }
    }