//注册 public Student regedit(String stuNo, String stuPass){ Student regeditStudent = null;//表示注册的学生对象,假设注册失败,所以是null //如果学生没有重复的,那么将学生信息注册到D://students.txt文件 File file = new File(STUDENT_MESS_PATH); if (file.length() == 0) { // 判断文件是否为空 Student student = new Student(stuNo,stuPass); students.add(student); IOUtils ioUtils = new IOUtils(); ioUtils.writerFileByXuLie(regeditStudent,STUDENT_MESS_PATH,true);//将学生信息写入文件 regeditStudent = student; } else { IOUtils ioUtils = new IOUtils(); ArrayList<Student> students = (ArrayList<Student>) ioUtils.readFileByXuLie(STUDENT_MESS_PATH); boolean isCHongFu = false;//没重复 for (Student everyStudent:students ) { if (everyStudent.getStuNo().equals(stuNo) && everyStudent.getStuPass().equals(stuPass)) { isCHongFu = true; regeditStudent = everyStudent; break; } } if (!isCHongFu) { Student student = new Student(stuNo,stuPass); students.add(student); ioUtils.writerFileByXuLie(regeditStudent,STUDENT_MESS_PATH,true);//将学生信息写入文件 regeditStudent = student; } } return regeditStudent; }补全
时间: 2023-06-16 09:03:32 浏览: 122
代码如下:
```
public Student regedit(String stuNo, String stuPass) {
Student regeditStudent = null;//表示注册的学生对象,假设注册失败,所以是null
//如果学生没有重复的,那么将学生信息注册到D://students.txt文件
File file = new File(STUDENT_MESS_PATH);
if (file.length() == 0) { // 判断文件是否为空
Student student = new Student(stuNo,stuPass);
students.add(student);
IOUtils ioUtils = new IOUtils();
ioUtils.writerFileByXuLie(students, STUDENT_MESS_PATH, true);//将学生信息写入文件
regeditStudent = student;
}
else {
IOUtils ioUtils = new IOUtils();
ArrayList<Student> students = (ArrayList<Student>) ioUtils.readFileByXuLie(STUDENT_MESS_PATH);
boolean isCHongFu = false;//没重复
for (Student everyStudent : students) {
if (everyStudent.getStuNo().equals(stuNo) && everyStudent.getStuPass().equals(stuPass)) {
isCHongFu = true;
regeditStudent = everyStudent;
break;
}
}
if (!isCHongFu) {
Student student = new Student(stuNo,stuPass);
students.add(student);
ioUtils.writerFileByXuLie(students, STUDENT_MESS_PATH, true);//将学生信息写入文件
regeditStudent = student;
}
}
return regeditStudent;
}
```
注释:修正了将学生信息写入文件时,写入了错误的对象和变量名的问题。另外,也将是否有重复的判断逻辑进行了修正,使其更加合理。
阅读全文