Browse Source

Added readFromFile Method to Student class

remotes/origin/fdai7780
fdai7887 11 months ago
parent
commit
10448e473d
  1. 28
      src/main/java/org/example/Student.java

28
src/main/java/org/example/Student.java

@ -1,8 +1,8 @@
package org.example; package org.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/** /**
* Represents an student user in the system. * Represents an student user in the system.
@ -75,5 +75,27 @@ public class Student {
} }
public static List<Student> readFromFile(String filename)
{
List<Student> students = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String name = line;
String id = reader.readLine();
String role = reader.readLine();
Student student = new Student(name, id, role);
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
} }
Loading…
Cancel
Save