属性类:
package information; //存放学生的属性 public class Student { private int id; private String name; private int age; 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; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
操作类:
package operate; import information.Student; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; //学生管理操作操作类,用于执行所有操作 public class Operate { private File file = new File("StudentDate");// 新建文件夹用于存储所有学生文件 // 查看全部学生信息 public List<Student> findAll() { // TODO Auto-generated method stub //创建一个集合,即查看全部学生信息变成直接遍历集合 List<Student> list = new ArrayList<Student>(); // 得到StudentDate下的所有文件 File[] f = file.listFiles(new FileFilter() { // 过滤器得到StudentDate下所有后缀名为".st"的文件 @Override public boolean accept(File pathname) { // TODO Auto-generated method stub return pathname.getName().endsWith(".st"); } }); for (File fs : f) { Student s = new Student(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(fs)); s.setId(Integer.parseInt(br.readLine()));// Integer.parseInt是将字符串变成int s.setName(br.readLine()); s.setAge(Integer.parseInt(br.readLine())); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (br != null) { //因为br初始化为null即流没开启,如果不等于null,才能关闭流 try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } list.add(s); //将id对应的Student对象添加到集合中,方便后边遍历 } return list; } // 查看是否存在输入的id public boolean findById(int id) { File f = new File(file, id + ".st"); return f.exists();//判断该id命名的文件是否存在 } // 添加学生操作 public boolean add(Student s) { if (findById(s.getId())) { return true; } else { BufferedWriter bw = null; try { File f = new File(file, s.getId() + ".st"); // 在StudentDate文件夹中创建一个文件 FileWriter fw = new FileWriter(f); // 向文件中写入信息 bw = new BufferedWriter(fw); // 包装流,提高效率 bw.write(String.valueOf(s.getId())); //因为id为int类型,所以需要转换成String类型才能写入 //getId是得到输入的id然后写入到文件中(下面同理) bw.newLine(); //写入一个行分隔符,即另起一行 bw.write(s.getName()); bw.newLine(); bw.write(String.valueOf(s.getAge())); bw.flush(); // 输出操作之后要进行刷新 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); // fw.close(); //不需要,因为bw包装了fw,即bw关闭fw就关闭 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return false; } //查找某个学生信息 public Student find(int id) { // TODO Auto-generated method stub Student s = new Student(); if(findById(id)){ //调用findById(),查找是否存在该学生 BufferedReader br = null; File f = new File(file,id+".st"); //用变量 f 定义该学生所对应文件 try { br = new BufferedReader(new FileReader(f)); s.setId(Integer.parseInt(br.readLine()));// Integer.parseInt是将字符串变成int s.setName(br.readLine()); s.setAge(Integer.parseInt(br.readLine())); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (br != null) { //br try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return s; }else{ return null; } } //删除某个学生信息 public boolean del(int id) { // TODO Auto-generated method stub if(findById(id)){ File f = new File(file,id+".st"); //创建文件 f.delete(); return true; }else{ return false; } } //修改某个学生信息 public boolean update(Student s) { // TODO Auto-generated method stub BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(new File(file,s.getId()+".st"))); bw.write(String.valueOf(s.getId())); bw.newLine(); bw.write(s.getName()); bw.newLine(); bw.write(String.valueOf(s.getAge())); bw.flush(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; }finally{ if(bw!=null){ try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
====================================================================
希望能与更多的java程序员共同学习
扣扣联系:2531977855