学生管理系统IO版
在集合版的基础上更新
- 从文件中读数据到集合中
-
public static void readData(String fileName,ArrayList<Student>arrayList) throws IOException { //创建输入缓冲流对象 BufferedReader bufferedReader=new BufferedReader(new FileReader(fileName)); String line; while ((line=bufferedReader.readLine())!=null) { String []datas=line.split(","); //数据以,隔开 Student student=new Student(); student.setId(datas[0]); student.setName(datas[1]); student.setAge(datas[2]); student.setAddress(datas[3]); arrayList.add(student); } bufferedReader.close(); //关闭资源 }
- 从集合中的数据写入文件
public static void writeData(String fileName,ArrayList<Student>arrayList) throws IOException { BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(fileName)); for(int i=0;i<arrayList.size();i++) { Student student=arrayList.get(i); //拿到每个数据 StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress()); bufferedWriter.write(stringBuilder.toString()); //变成字符串接收 bufferedWriter.newLine(); bufferedWriter.flush(); } bufferedWriter.close(); }
集合和IO目前是两个独立版本,需要把俩个版本关联起来
- 把集合替换成文本
package test; 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 { // TODO Auto-generated method stub //定义文件取代集合 String flieName="students.txt"; Scanner scanner=new Scanner(System.in); 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.println("请输入您的选择:"); int choice=scanner.nextInt(); //定义一个int类型数据接收控制台所输出的数组,来判断是方法几 switch (choice) { case 1: System.out.println("显示所有学生信息"); findStudent(flieName);//因为学生信息存储到集合中,调用集合对象 break; case 2: System.out.println("添加学生信息"); addStudent(flieName); break; case 3: System.out.println("删除学生信息"); deleteStudent(flieName); break; case 4: System.out.println("修改学生信息"); revampStudent(flieName); break; case 5: System.out.println("退出"); System.exit(0);//jvm退出 break; default: System.out.println("您输入的数字有误,请重新输入!"); break; } } } //显示所有学生信息 public static void findStudent(String fileName) throws IOException{ //创建一个集合对象 ArrayList<Student>arrayList=new ArrayList<>(); //从文件中把数据读取到集合中 readData(fileName, arrayList); //首先判断集合中是否有数据,如果没有数据则提示并退出,重新选择功能 if (arrayList.size()==0) { System.out.println("目前没有学生信息存在"); return;//不返回值,单纯当作退出功能使用 } //循环遍历拿出学生信息 System.out.println("学号\t姓名\t年龄\t地址"); for(int i=0;i<arrayList.size();i++){ //创建一个Student对象接收 Student student=new Student(); student=arrayList.get(i);//拿到值 System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress()); } } //添加学生信息功能 public static void addStudent(String fileName) throws IOException{ //创建一个集合对象 ArrayList<Student>arrayList=new ArrayList<>(); //从文件中把数据读取到集合中 readData(fileName, arrayList); Scanner scanner=new Scanner(System.in); //判读ID是唯一的 String id; while(true){ boolean flag=false; System.out.println("请输入学生学号"); id=scanner.nextLine(); //拿到ID 去判断是否重复 for(int i=0;i<arrayList.size();i++) { Student student; student=arrayList.get(i); if (student.getId().equals(id)) { //判断ID是否重复 flag=true; break; } } if (flag) { System.out.println("您输入的id重复,请重新输入!"); }else { break; } } System.out.println("请输入学生的姓名:"); String name=scanner.nextLine(); System.out.println("请输入学生的年龄"); String age=scanner.nextLine(); System.out.println("请输入学生的地址"); String address=scanner.nextLine(); //创建 一个学生类的对象接收 Student student=new Student(); student.setId(id); student.setName(name); student.setAge(age); student.setAddress(address); //让后把数据传入集合中 arrayList.add(student); //把数据重新写如到文件中 writeData(fileName, arrayList); System.out.println("学生信息已存储!"); } //删除学生信息 public static void deleteStudent(String fileName) throws IOException { //创建一个集合对象 ArrayList<Student>arrayList=new ArrayList<>(); //从文件中把数据读取到集合中 readData(fileName, arrayList); //通过ID删除学生信息,判断控制台输入的id与集合中的id是否一致 Scanner scanner=new Scanner(System.in); System.out.println("输入您要删除的学生ID"); String id=scanner.nextLine(); int index=-1; for(int i=0;i<arrayList.size();i++) { Student student=arrayList.get(i); if (student.getId().equals(id)) { index=i; break; } } System.err.println(index); if (index==-1) { System.out.println("您要删除的数据不存在!请重新选择"); }else { arrayList.remove(index); //把数据重新写如到文件中 writeData(fileName, arrayList); System.out.println("删除成功"); } } //修改学生信息 public static void revampStudent(String fileName) throws IOException { //创建一个集合对象 ArrayList<Student>arrayList=new ArrayList<>(); //从文件中把数据读取到集合中 readData(fileName, arrayList); //通过ID删除学生信息,判断控制台输入的id与集合中的id是否一致 Scanner scanner=new Scanner(System.in); System.out.println("输入您要修改的学生ID"); String id=scanner.nextLine(); int index=-1; for(int i=0;i<arrayList.size();i++) { Student student=arrayList.get(i); if (student.getId().equals(id)) { index=i; break; } } if (index==-1) { System.out.println("您要修改的数据不存在!请重新选择"); }else { System.out.println("请输入学生新ID"); id=scanner.nextLine(); System.out.println("请输入学生的新姓名:"); String name=scanner.nextLine(); System.out.println("请输入学生的新年龄"); String age=scanner.nextLine(); System.out.println("请输入学生的新地址"); String address=scanner.nextLine(); //创建 一个学生类的对象接收 Student student=new Student(); student.setId(id); student.setName(name); student.setAge(age); student.setAddress(address); //让后把数据传入集合中 arrayList.set(index, student); //创建集合新的数据 //把数据重新写如到文件中 writeData(fileName, arrayList); System.out.println("学生信息已修改!"); } } //从文件中读数据到集合中 public static void readData(String fileName,ArrayList<Student>arrayList) throws IOException { //创建输入缓冲流对象 BufferedReader bufferedReader=new BufferedReader(new FileReader(fileName)); String line; while ((line=bufferedReader.readLine())!=null) { String []datas=line.split(","); //数据以,隔开 Student student=new Student(); student.setId(datas[0]); student.setName(datas[1]); student.setAge(datas[2]); student.setAddress(datas[3]); arrayList.add(student); } bufferedReader.close(); //关闭资源 } //从集合中的数据写入文件 public static void writeData(String fileName,ArrayList<Student>arrayList) throws IOException { BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(fileName)); for(int i=0;i<arrayList.size();i++) { Student student=arrayList.get(i); //拿到每个数据 StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress()); bufferedWriter.write(stringBuilder.toString()); //变成字符串接收 bufferedWriter.newLine(); bufferedWriter.flush(); } bufferedWriter.close(); } }