StudentManagerTest类
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagerTest {
public static void main(String[] args) throws IOException {
//定义文件路径
String fileName="student.txt";
//为了创建student.txt文件
File f=new File("student.txt");
f.createNewFile();
while(true) {
System.out.println("-------欢迎来到学生管理系统-------");
System.out.println("1查看所有学生");
System.out.println("2添加学生");
System.out.println("3删除学生");
System.out.println("4修改学生");
System.out.println("5退出");
System.out.print("请输入你的选择:");
Scanner scan = new Scanner(System.in);
String choice=scan.nextLine();
//switch语句实现
switch(choice) {
case "1":
//查看所有学生
findAllStudent(fileName);
break;
case "2":
//添加学生
addStudent(fileName);
break;
case "3":
//删除学生
deleteStudent(fileName);
break;
case "4":
//修改学生
updateStudent(fileName);
break;
case "5":
//退出
default:
//输入有误
System.out.println("谢谢你的使用");
System.exit(0);
break;
}
}
}
//查看所有学生
public static void findAllStudent(String fileName) throws IOException {
ArrayList<Student> stus=new ArrayList<Student>();
readData(fileName,stus);
//先判断集合是否有数据
if(stus.size()==0) {
System.out.println("目前没有学生数据可查询");
return ;
}
System.out.println("学号\t姓名\t年龄\t地址");
for (int i = 0; i < stus.size(); i++) {
Student s=stus.get(i);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
//添加学生
public static void addStudent(String fileName) throws IOException {
ArrayList<Student> stus=new ArrayList<Student>();
readData(fileName,stus);
Scanner scan=new Scanner(System.in);
//判断学号有没有被占用
String id;
while(true) {
System.out.print("请输入学生学号:");
id=scan.nextLine();
boolean flag=false;
for (int i = 0; i < stus.size(); i++) {
Student s=stus.get(i);
if(s.getId().equals(id)) {
flag=true;
break;
}
}
if(flag) {
System.out.println("你输入的学号已经被占用,请重新输入");
}else {
break;
}
}
System.out.print("请输入学生姓名:");
String name=scan.nextLine();
System.out.print("请输入学生年龄:");
String age=scan.nextLine();
System.out.print("请输入学生地址:");
String address=scan.nextLine();
//创建一个学生对象
Student stu=new Student(id,name,age,address);
//添加到学生集合中
stus.add(stu);
System.out.println("添加成功");
writeData(fileName,stus);
}
//删除学生(根据学号)
public static void deleteStudent(String fileName) throws IOException {
ArrayList<Student> stus=new ArrayList<Student>();
readData(fileName,stus);
System.out.println("请输入要删除的学生的学号:");
Scanner scan=new Scanner(System.in);
String id=scan.nextLine();
int index=-1;
for (int i = 0; i < stus.size(); i++) {
Student s=stus.get(i);
if(s.getId().equals(id)) {
index=i;
break;
}
}
if(index!=-1) {
stus.remove(index);
System.out.println("删除成功");
}else {
System.out.println("未找到该学号的学生");
}
writeData(fileName,stus);
}
//修改学生
public static void updateStudent(String fileName) throws IOException {
ArrayList<Student> stus=new ArrayList<Student>();
readData(fileName,stus);
System.out.println("请输入要修改学生的学号:");
Scanner scan=new Scanner(System.in);
String id=scan.nextLine();
int index=-1;
for (int i = 0; i < stus.size(); i++) {
Student s=stus.get(i);
if(s.getId().equals(id)) {
index=i;
break;
}
}
if(index==-1) {
System.out.println("对不起,您要修改的学生的学号不存在");
}else {
Student s=stus.get(index);
System.out.print("请输入学生新姓名:");
String name=scan.nextLine();
System.out.print("请输入学生新年龄:");
String age=scan.nextLine();
System.out.print("请输入学生新地址:");
String address=scan.nextLine();
s.setName(name);
s.setAge(age);
s.setAddress(address);
System.out.println("修改学生成功");
}
writeData(fileName,stus);
}
//从文件中读数据
public static void readData(String fileName,ArrayList<Student> array) throws IOException{
BufferedReader br=new BufferedReader(new FileReader(fileName));
String line;
while((line=br.readLine())!=null) {
String[] datas=line.split(",");
Student s=new Student(datas[0],datas[1],datas[2],datas[3]);
array.add(s);
}
br.close();
}
//把集合中的数据写入文件
public static void writeData(String fileName,ArrayList<Student> array) throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < array.size(); i++) {
Student s=array.get(i);
StringBuilder sb=new StringBuilder();
sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
bw.write(sb.toString());
bw.flush();
}
bw.close();
}
}
Student类
public class Student {
private String id;
private String name;
private String age;
private String address;
public Student() {
}
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
使用前先在项目的根目录创建一个student.txt文件