本文共 5534 字,大约阅读时间需要 18 分钟。
标准学生类:
package 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) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } //get/set 方法 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 studentmanager;
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; import student.Student; //學生类测试类 public class StudentManager { // 主方法 public static void main(String[] args) throws IOException { // 为了能一直返回到主界面 用死循环实现 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("请输入你的选择:"); // 创建键盘录入对象 Scanner sc = new Scanner(System.in); String choiceNumber = sc.nextLine(); //定义文件路径 String fileName="student.txt"; // 用switch语句写选择 switch (choiceNumber) { case "1":findAllStudent(fileName); break;// 查看所有学生信息 case "2":addStudent( fileName); break;// 添加学生信息 case "3":deletStudent(fileName); break;// 删除学生信息 case "4":updateStudent(fileName); break;// 修改学生信息 case "5": default: System.out.println("谢谢使用!"); System.exit(0);// 退出程序 break;// 退出系统 } } }
//编写读取文件中信息的方法
public static void read(String fileName,ArrayList<Student>array) throws IOException{ //创建输入缓冲流对象 BufferedReader br=new BufferedReader(new FileReader(fileName)); String line; while((line=br.readLine())!=null){ String []chs=line.split(","); //创建学生类对象 Student s=new Student(chs[0],chs[1],chs[2],chs[3]); //将学生对象加入集合中 array.add(s); } br.close(); }
//编写写文件的方法
public static void write(String fileName,ArrayList<Student>array) throws IOException{ //创建输出缓冲流对象 BufferedWriter bw=new BufferedWriter(new FileWriter(fileName)); //遍历集合 将信息写入文件 for (int i = 0; i < array.size(); i++) { //先定义一个StringBuilder 对象 StringBuilder sb=new StringBuilder(); sb.append(array.get(i).getId()).append(",").append(array.get(i).getName()).append(",").append(array.get(i).getAge()).append(",").append(array.get(i).getAddress()); bw.write(sb.toString()); bw.newLine(); bw.flush(); } bw.close(); }
// 编写查看所有学生信息的方法
public static void findAllStudent(String fileName) throws IOException { //创建学生类集合对象 ArrayList<Student>array=new ArrayList<Student>(); read( fileName,array); System.out.println("学号\t\t姓名\t年龄\t居住地"); // for循环遍历集合 for (int i = 0; i < array.size(); i++) { // 创建学生对象 Student s = new Student(); s = array.get(i); // 打印学生信息 System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress()); } }
//添加学生信息
public static void addStudent(String fileName) throws IOException{ //创建学生类集合对象 ArrayList<Student>array=new ArrayList<Student>(); read( fileName,array); //创建键盘录入对象 Scanner sc=new Scanner(System.in); //为了能访问到flag 先定义一个flag boolean flag; //为了能访问到id 先定义一个id String id; //为了能有多次输入机会 用while循环实现 while(true){ //提示输入学生学号 System.out.println("请输入学生学号:"); //接收数据 id=sc.nextLine(); //定义一个flag flag=false; //遍历集合 比较输入的学号是否已存在 for (int i = 0; i < array.size(); i++) { if(id.equals(array.get(i).getId())){ flag=true; break; } } if(flag){ System.out.println("你输入的学号已存在,请重新输入"); }else{ break; } } if(flag==false){ //提示输入学生姓名 System.out.println("请输入学生姓名"); String name=sc.nextLine(); //输入学生年龄 System.out.println("请输入学生年龄"); String age=sc.nextLine(); //输入学生居住地 System.out.println("请输入学生居住地"); String address=sc.nextLine(); //创建学生对象 Student s=new Student(id,name,age,address); //将学生对象添加到集合中 array.add(s); write(fileName,array); System.out.println("添加学生成功!"); } }
//编写删除学生信息的方法
public static void deletStudent(String fileName) throws IOException{ //创建学生类集合对象 ArrayList<Student>array=new ArrayList<Student>(); read( fileName,array); //创建键盘录入对象 Scanner sc=new Scanner(System.in); //提示输入学生学号 System.out.println("请输入要删除的学号"); String id=sc.nextLine(); //先定义一个索引 int index=-1; //遍历集合 看是否有输入的学号的学生 for (int i = 0; i < array.size(); i++) { if(id.equals(array.get(i).getId())){ index=i; } } if(index==-1){ System.out.println("你要删除的学号对应的学生信息不存在"); return; }else{ array.remove(index); write(fileName,array); System.out.println("删除成功!"); } }
//編寫修改学生信息的方法
public static void updateStudent(String fileName) throws IOException{ //创建学生类集合对象 ArrayList<Student>array=new ArrayList<Student>(); read(fileName,array); //创建键盘录入对象 Scanner sc=new Scanner(System.in); //提示输入要修改的学生学号 System.out.println("请输入要修改的学生学号"); String id=sc.nextLine(); //先定义一个索引号 int index=-1; //遍历集合,找出相对应的学生 for (int i = 0; i < array.size(); i++) { if(id.equals(array.get(i).getId())){ index=i; } } if(index==-1){ System.out.println("没找到你要修改的学生信息"); }else{ //提示输入修改后的姓名 System.out.println("请输入修改后的姓名"); String name=sc.nextLine(); //提示输入修改后的年龄 System.out.println("请输入修改后的年龄"); String age=sc.nextLine(); //提示输入修改后的学生居住地 System.out.println("请输入修改后的学生居住地"); String address=sc.nextLine(); //创建学生对象 Student s=new Student(id,name,age,address); //修改学生信息 array.set(index, s); write(fileName,array); System.out.println("修改成功!"); } } }转载地址:http://dalti.baihongyu.com/