学生信息存储在TXT文件中,学生的每个信息通过空格分开,一个学生信息独占一行存储。
我们需要对信息进行基本的,增、删、改、查,全部显示操作。
学生类/Student
package com.fr.Text; public class Student { /** 学生id */ private int id; /** 学生姓名 */ private String name; /** 学生年龄 */ private int age; /** 学生性别 */ private String sex; /** * 无参构造方法 */ public Student() { } /** * 有参构造方法 */ public Student(int id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } /** * 重写的toString方法 */ @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } /** * 重写的hashCode方法 */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); return result; } /** * 重写的equals方法 */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex == null) { if (other.sex != null) return false; } else if (!sex.equals(other.sex)) return false; return true; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
学生管理类的接口定义/StudentManagement
package com.fr.Text; import java.io.File; import java.util.List; public interface StudentManagement { /** 默认路径 */ public static final String DEFAULT_PATH = System.getProperty("user.dir") + File.separator + "Student.txt"; /** * 取出文件中所有学生的信息 * * @return 学生类的集合,如果文件中没有就返回null */ public List<Student> list(); /** * 向文件中添加一个学生信息, 如果文件中已经存在传入对象的id则添加失败。 * * @param student * 学生的对象 * @return 添加成功返回true,添加失败返回false */ public boolean increase(Student student); /** * 根据输入的id删除文件中的学生信息 * * @param id * 学生的id值 * @return true/删除成功 false/删除失败或者文件中没有该学生id */ public boolean delete(int id); /** * 修改学生的属性,根据传入学生的id值,查找学生,然后将新学生数据替换此数据 * * @param student * 学生的对象 * @return true/修改成功 false/修改失败,或者文件中没有该学生id的学生 */ public boolean change(Student student); /** * 根据传入的id,在文件中查找学生的属性 * * @param id * 学生的id * @return 对应id的学生对象,没有则返回null */ public Student CheckId(int id); /** * 根据传入对象的数据查找是否有该对象 * * @param student * 学生对象 * @return true/找到返回 false/没找到 */ public boolean CheckStudent(Student student); }
学生管理类接口的实现类/MyStudentManagement
package com.fr.Text; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class MyStudentManagement implements StudentManagement { /** 文件路径 */ private String CustomPath; /** * 默认路径下查找文件, 如果没有找到就创建一个 */ public MyStudentManagement() { this.CustomPath = DEFAULT_PATH; File file = null; try { file = new File(this.CustomPath); if (!(file.exists())) { file.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } } /** * 自定义路径下查找文件, 如果没有找到就创建一个 */ public MyStudentManagement(String CustomPath) { this.CustomPath = CustomPath; File file = null; try { file = new File(this.CustomPath); if (!(file.exists())) { file.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } } @Override public List<Student> list() { List<Student> lists = new ArrayList<>(); BufferedReader bis = null; try { bis = new BufferedReader(new FileReader(this.CustomPath)); String str = null; while ((str = bis.readLine()) != null) { String[] stus = str.split("\s+"); lists.add(new Student(new Integer(stus[0]), stus[1], new Integer(stus[2]), stus[3])); } return lists; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override public boolean increase(Student student) { boolean flag = false; PrintWriter pw = null; try { pw = new PrintWriter(new FileOutputStream(CustomPath, true)); List<Student> lists = list(); for (int i = 0; i < lists.size(); i++) { if (lists.get(i).getId() == student.getId()) { return false; } } pw.println((student.getId() + " " + student.getName() + " " + student.getAge() + " " + student.getSex())); flag = true; } catch (FileNotFoundException e) { flag = false; e.printStackTrace(); } finally { try { if (pw != null) { pw.close(); } } catch (Exception e) { e.printStackTrace(); } } return flag; } @Override public boolean delete(int id) { boolean flag = false; PrintWriter pw = null; try { List<Student> lists = list(); for (int i = 0; i < lists.size(); i++) { Student student = lists.get(i); if (student.getId() == id) { lists.remove(student); flag = true; break; } } if (flag) { pw = new PrintWriter(CustomPath); for (int i = 0; i < lists.size(); i++) { Student student = lists.get(i); pw.println((student.getId() + " " + student.getName() + " " + student.getAge() + " " + student.getSex())); } } } catch (FileNotFoundException e) { flag = false; e.printStackTrace(); } finally { try { if (pw != null) { pw.close(); } } catch (Exception e) { e.printStackTrace(); } } return flag; } @Override public boolean change(Student student) { boolean flag = false; PrintWriter pw = null; try { List<Student> lists = list(); for (int i = 0; i < lists.size(); i++) { if (lists.get(i).getId() == student.getId()) { lists.get(i).setName(student.getName()); lists.get(i).setAge(student.getAge()); lists.get(i).setSex(student.getSex()); flag = true; break; } } if (flag) { pw = new PrintWriter(CustomPath); for (int i = 0; i < lists.size(); i++) { increase(lists.get(i)); } } } catch (FileNotFoundException e) { flag = false; e.printStackTrace(); } finally { try { if (pw != null) { pw.close(); } } catch (Exception e) { e.printStackTrace(); } } return flag; } @Override public Student CheckId(int id) { List<Student> lists = list(); for (int i = 0; i < lists.size(); i++) { if (lists.get(i).getId() == id) { return lists.get(i); } } return null; } @Override public boolean CheckStudent(Student student) { List<Student> lists = list(); for (int i = 0; i < lists.size(); i++) { if (student.equals(lists.get(i))) { return true; } } return false; } }