Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StudentList {
- private ArrayList<Student> students = new ArrayList<Student>();
- private File file;
- private ArrayList<Student> readAll() {
- ArrayList arrStudent = new ArrayList();
- try {
- RandomAccessFile randomAccess = new RandomAccessFile(file, "r");
- String r = "";
- while ((r = file.readLine()) != null) {
- Student std = new Student();
- String[] str = r.split("\\|");
- std.setId(Integer.parseInt(str[0].toString()));
- std.setName(str[1].toString());
- std.setScore(Float.parseFloat(str[2].toString()));
- arrStudent.add(std);
- }
- randomAccess.close();
- } catch (IOException | NumberFormatException ex) {
- System.out.println(ex.getMessage());
- }
- return arrStudent;
- }
- public StudentList() {
- file = new File("D:\\Ass.std");
- students = readAll();
- }
- public StudentList(string directory) {
- file = new File(directory);
- students = readAll();
- }
- public void list() {
- int count = 0;
- for (Student s : std) {
- System.out.println((++count) + ".ID: " + s.getId() + " Name: " + s.getName() + " Score: " + s.getScore());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement