package day11_io_student.student_demo;
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; } }
将学生管理方法中产生的数据记录(读写)到文件中。
package day11_io_student.student_demo; import java.io.*; import java.util.ArrayList; import java.util.Scanner; /* * iostudent增加了read和write两个方法,实现读写数据到文件的操作 * 除了查看学生只用到read方法,其他几个都对文件进行了改动所以在结束时要将改动部分写入文件中 * read、write方法和文件、集合关联(即参数引用)。增删改程序和文件关联。 * ArrayListd对象从只在main中new改为在各个方法中去new * br和bw记得throw exception,close file*/ public class IoStudentManager { public static void main(String[] args) throws IOException { // ArrayList<Student> fileName = new ArrayList<Student>(); String fileName = "student.txt"; while(true) { System.out.println("1 查看学生"); System.out.println("2 添加学生"); System.out.println("3 修改学生"); System.out.println("4 删除学生"); System.out.println("5 退出"); System.out.println("请输入你的选择:"); Scanner sc = new Scanner(System.in); String num = sc.nextLine(); switch(num) { case "1": findAllStatus(fileName); break; case "2": addStatus(fileName); break; case "3": updateStatus(fileName); break; case "4": deleteStatus(fileName); break; case "5": default: System.out.println("谢谢使用!"); System.exit(0); break; } } } //从集合写数据到文件 public static void writeData(String fileName,ArrayList<Student> arrayli) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); for(int i=0;i<arrayli.size();i++) { Student sn = arrayli.get(i); StringBuilder sb = new StringBuilder(); sb.append(sn.getId()).append(",").append(sn.getName()).append(",").append(sn.getAge()).append(",").append(sn.getAddress()); bw.write(sb.toString()); bw.newLine(); bw.flush(); } bw.close(); } //从文件读数据到集合 public static void readData(String fileName,ArrayList<Student> arrayli) throws IOException{ BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while((line=br.readLine())!=null) { String[] datasarray = line.split(","); Student st = new Student(datasarray[0],datasarray[1],datasarray[2],datasarray[3]); arrayli.add(st); } br.close(); } //查看学生 public static void findAllStatus(String fileName) throws IOException { ArrayList<Student> arrayli = new ArrayList<Student>(); //从文件读取数据到集合 readData(fileName,arrayli); if(arrayli.size()==0) { System.out.println("学生不存在,请添加"); return; } System.out.println("学号"+" "+"姓名"+" "+"年龄"+" "+"地址"); for(int i=0;i<arrayli.size();i++) { Student st = arrayli.get(i); System.out.println(st.getId()+" "+st.getName()+" "+st.getAge()+" "+st.getAddress()); } } //添加学生 public static void addStatus(String fileName) throws IOException { ArrayList<Student> arrayli = new ArrayList<Student>(); //从文件中读数据到集合 readData(fileName,arrayli); Scanner sc = new Scanner(System.in); String ids; //判断id是否重复 while(true) { System.out.println("请输入学号:"); ids = sc.nextLine(); //从集合中遍历学号,看学号是否已经存在 boolean tag=false; for(int i=0;i<arrayli.size();i++) { Student sn = arrayli.get(i); if(sn.getId().equals(ids)) { tag = true; break; } } if(tag) { System.out.println("学号已经存在,请重新输入!"); }else { break; } } System.out.println("请输入姓名:"); String names = sc.nextLine(); System.out.println("请输入年龄:"); String ages = sc.nextLine(); System.out.println("请输入地址:"); String adds = sc.nextLine(); //添加学生 Student st = new Student(ids,names,ages,adds); arrayli.add(st); //把集合的数据重新写会文件 writeData(fileName,arrayli); System.out.println("添加学生成功!"); } //修改学生 public static void updateStatus(String fileName) throws IOException { ArrayList<Student> arrayli = new ArrayList<Student>(); //从文件中读数据到集合 readData(fileName,arrayli); Scanner sc = new Scanner(System.in); String ids; while(true) { System.out.println("请输入要改的学号:"); ids = sc.nextLine(); int len = -1; for(int i=0;i<arrayli.size();i++) { Student sn = arrayli.get(i); if(sn.getId().equals(ids)) { len = i; } } if(len==-1) { System.out.println("学生不存在"); }else { System.out.println("请输入修改姓名:"); String names = sc.nextLine(); System.out.println("请输入修改年龄:"); String ages = sc.nextLine(); System.out.println("请输入修改地址:"); String adds = sc.nextLine(); //修改学生 Student st = new Student(ids,names,ages,adds); arrayli.set(len,st); //把集合数据重新写回文件 writeData(fileName,arrayli); System.out.println("修改学生成功!"); break; } } } //删除学生 public static void deleteStatus(String fileName) throws IOException { ArrayList<Student> arrayli = new ArrayList<Student>(); //从文件中读数据到集合 readData(fileName,arrayli); Scanner sc = new Scanner(System.in); String ids; while(true) { System.out.println("请输入要删除的学生:"); ids = sc.nextLine(); int num = -1; for(int i=0;i<arrayli.size();i++) { if(arrayli.get(i).getId().equals(ids)) { num = i; } } if(num==-1) { System.out.println("学生不存在!"); }else { arrayli.remove(num); //把集合中的数据重新写回到文件 writeData(fileName, arrayli); System.out.println("deleted!"); break; } } } }