Java - Comment lire fichier dans la liste de tableaux d'Objets?

Je veux faire un ArrayList de l'Étudiant et l'enregistrer dans un fichier pour une utilisation ultérieure. J'ai réussi a écrit, mais quand je l'ai lu de retour à ArrayList, je n'ai qu'un seul Objet.

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!");
}

Quand j'ai lu le fichier en appelant getStudent() et de l'imprimer par listStudent() je n'ai qu'un seul enregistrement du fichier.

S'il vous plaît aidez-moi!
Beaucoup apprécient.

MODIFIER

J'avais essayé d'écrire un arraylist de fichier et de le lire dans arraylist. Je vais vous montrer comment je l'ai fait.
Tout d'abord, j'écris arraylist de fichier:

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!");

Et puis j'ai lu le fichier étudiant:

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;
}
}

Je peux voir que dans le fichier, j'ai de nombreux objets que la taille du fichier de poursuivre sa croissance. Mais j'ai seulement un objet après l'avoir lu, ce qui est mon tout premier objet que j'ai écrit dans un fichier.

Vous n'apparaissez à écrire un élève de fichier. Il est donc logique que vous allez lire seul.
Merci pour votre commentaire. J'ai remarqué que, cependant j'ai ajouté le nouvel objet à l'ancien fichier, donc, techniquement, j'ai des tas d'objets dans mon fichier. FileOutputStream fos = new FileOutputStream(chemin d'accès, true);

OriginalL'auteur TomNg | 2013-11-20