我想创建一个学生的 ArrayList
并将其保存到文件以供以后使用。我成功写入了它,但是当我将其读回到 ArrayList
时,我只有一个对象。
public class Student implements Serializable{
public String fname, lname, course;
int section;
private static final long serialVersionUID = 1L;
public static ArrayList<Student> students = getStudent();
public Student() {
}
public Student(String fname, String lname, String course, int section){
this.fname = fname;
this.lname = lname;
this.course = course;
this.section = section;
}
public static void addStudent(){
String fname = GetInput.getInput("Enter the First Name: ");
String lname = GetInput.getInput("Enter the Last Name: ");
String course = GetInput.getInput("Enter the Course: ");
String S_section = GetInput.getInput("Enter the section: ");
int section = Integer.parseInt(S_section);
Student student = new Student(fname, lname, course, section);
students.add(student);
System.out.println("Writing to file...");
try {
writeToFile(student);
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public static ArrayList<Student> getStudent(){
try{
FileInputStream fis = new FileInputStream("C:\\students.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Student> students1 = (ArrayList<Student>) ois.readObject();
ois.close();
return students1;
} catch( ClassNotFoundException | IOException ex){
System.out.println(ex.getMessage());
return null;
}
}
public static void listStudent(ArrayList<Student> students){
System.out.println("View the Records in the Database:");
for(Student student: students){
System.out.println("Name: " + student.fname + " " + student.lname);
System.out.println("Course: " + student.course);
System.out.println("Section: " + student.section);
System.out.println();
}
}
static void writeToFile(Student student) throws FileNotFoundException, IOException{
String path = "C:\\students.ser";
FileOutputStream fos = new FileOutputStream(path, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student);
oos.close();
System.out.println("New Record has been written!");
}
当我通过调用
getStudent()
读取文件并通过listStudent()
输出时,只有一个记录的文件。
请帮帮我!非常感谢。编辑: 我试过将
arraylist
写入文件并将其读入arraylist
。我会向您展示我是如何做到这一点的。
首先,我将arraylist
写入文件:public static ArrayList<Student> students = new ArrayList<>();
public static void addStudent(){
Student student = new Student(fname, lname, course, section);
students.add(student);
System.out.println("Writing to file...");
try {
writeToFile(students);
}catch...
}
static void writeToFile(ArrayList<Student> students) throws FileNotFoundException, IOException{
String path = "C:\\students.ser";
FileOutputStream fos = new FileOutputStream(path, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(students);
oos.close();
System.out.println("New Record has been written!");
然后我阅读了学生档案:
public static ArrayList<Student> getStudent(){
try{
FileInputStream fis = new FileInputStream("C:\\students.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Student> students1 = (ArrayList<Student>) ois.readObject();
ois.close();
return students1;
} catch( ClassNotFoundException | IOException ex){
System.out.println(ex.getMessage());
return null;
}
}
我发现随着文件大小的增长,文件中有很多对象。但是在读取后,我只看到了一个对象,那就是我最初写入文件的对象。