在TeacherInformation.java这里的学生添加无法写入 学生.txt
package dzy;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class TeacherInformation {
private String name;
private String age;
private String account;
private String password;
ArrayList<Student> array = new ArrayList<>();
static Scanner scan=new Scanner(System.in);
@Override
public String toString() {
return "TeacherInformation [name=" + name + ", age=" + age + ", account=" + account + ", password=" + password
+ "]";
}
public TeacherInformation() {
super();
}
public TeacherInformation(String name, String age, String account, String password) {
super();
this.name = name;
this.age = age;
this.account = account;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
void AdmLogin() {
BufferedReader br = null;
try {
String str;
br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
while((str=br.readLine())!=null) {
byte[] bytes = str.getBytes();
str = new String(bytes,"UTF-8");
String num = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String name = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String sid = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String dorm = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String classes = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String score = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String password = str;
array.add(new Student(num,name,sid,dorm,classes,score,password));
}
}catch(Exception e1 ) {
System.out.println("读取失败");
e1.printStackTrace();
}finally {
if(br !=null) {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
while(true) {
System.out.println("**************教师管理系统****************");
System.out.println("\t 0:退出系统并保存\t");
System.out.println("\t 1:查看个人信息\t");
System.out.println("\t 2:修改密码\t");
System.out.println("\t 3:添加学生信息\t");
System.out.println("\t 4:查询某个学生的成绩.");
System.out.println("\t 5.查询某个学生的信息");
System.out.println("\t 6.查看所有学生的信息");
System.out.println("\t 7:修改学生的成绩.");
System.out.println("*****************************************");
System.out.println("请输入0~7");
String line=scan.nextLine();
switch(line){
case"1":
System.out.println("用户名:"+name+"密码:"+password+"年龄:"+age+"账号:"+account);
break;
case"2":
System.out.println("输入你的新密码:");
String str = scan.nextLine();
password = str;
System.out.println("修改成功,新密码为:"+password);
break;
case"3":
addStudent();
break;
case"4":
System.out.println("输入要查询学生的学号");
String tsid = scan.nextLine();
int i;
for( i = 0;i<array.size();i++) {
if(tsid.equals(array.get(i).getSid())) {
break;
}
}
if(i>=array.size()) {
System.out.println("学生不存在");
}else {
System.out.println("该学生的成绩"+array.get(i).getScore());
}
break;
case"5":
System.out.println("输入查询学生的学号:");
String tsid1 = scan.nextLine();
int i1;
for( i1 = 0;i1<array.size();i1++) {
if(tsid1.equals(array.get(i1).getSid())) {
break;
}
}
if(i1>=array.size()) {
System.out.println("学生不存在");
}else {
System.out.println("该学生的成绩:"+array.get(i1).toString());
}
break;
case"6":
for(int j = 0;j<array.size();j++) {
System.out.println("学生:"+array.get(j).getName()+"成绩:"+array.get(j).getScore());
}
break;
case"7":
updateStudent();
break;
case"0":
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter wr = null;
try {
String strs;
br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
while((str=br.readLine())!=null) {
System.out.println(str);
System.out.println(str.length());
byte[] bytes = str.getBytes();
str = new String(bytes,"UTF-8");
String num = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String name = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String sid = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String dorm = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String classes = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String score = str.substring(0,str.indexOf(","));
str = str.substring(str.indexOf(",")+1);
String password = str;
array.add(new Student(num,name,sid,dorm,classes,score,password));
}
}catch(Exception e1 ) {
System.out.println("读取失败");
e1.printStackTrace();
}finally {
if(br !=null) {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
void addStudent() {
System.out.println("输入用户名:");
String name =scan.nextLine();
for(int i = 0 ;i<array.size();i++) {
if(name.equals(array.get(i).getName())) {
System.out.println("用户名已存在!");
return;
}
}
System.out.println("输入序号:");
String num = scan.nextLine();
System.out.println("输入学号:");
String sid = scan.nextLine();
System.out.println("输入宿舍:");
String dorm = scan.nextLine();
System.out.println("输入班级:");
String classes = scan.nextLine();
System.out.println("输入成绩:");
String score = scan.nextLine();
System.out.println("输入密码:");
String password = scan.nextLine();
array.add(new Student(num,name,sid,dorm,classes,score,password));
System.out.println("添加成功!");
}
//修改学生信息
public void updateStudent(){
Scanner sc=new Scanner(System.in);
if(array.size()==0){
System.out.println("无信息,请先添加信息再查询!");
return;
}
System.out.println("请输入你要修改的学生学号:");
String sid=sc.nextLine();
System.out.println("输入序号:");
String num = scan.nextLine();
System.out.println("输入姓名:");
String name = scan.nextLine();
System.out.println("输入宿舍:");
String dorm = scan.nextLine();
System.out.println("输入班级:");
String classes = scan.nextLine();
System.out.println("输入成绩:");
String score = scan.nextLine();
System.out.println("输入密码:");
String password = scan.nextLine();
Student s=new Student();
s.setNum(num);
s.setName(name);
s.setSid(sid);
s.setDorm(dorm);
s.setCclass(classes);
s.setScore(score);
s.setPassword(password);
for(int i=0;i<array.size();i++) {
Student student=array.get(i);
if(student.getSid().equals(sid)) {
array.set(i, s);
System.out.println("修改学生信息成功!");
return;
}
}
System.out.println("输入学生信息有误,请重新输入!");
}
}