Java中流的操作练习
文件中的学生信息
学生信息存储在TXT文件中,我们需要对信息进行基本的,增、删、改、查,全部显示操作。
1、学生类/Student
package com.yujiao.student; public class Student { /**学生id*/ 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; } /** * 无参构造方法 */ public Student() { } /** * 有参构造方法 */ public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } /** * 重写的toString方法 */ @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
2、学生管理类的接口定义/StudentManagement
1 package com.yj.student; 2 3 import java.util.List; 4 5 public interface IStudentManager { 6 7 /** 默认文件存放位置*/ 8 public String DEFAULT_PATH = "E:\\Test\\student.txt"; 9 10 /** 11 * 获得所有学生信息 12 * @return 学生信息的List泛型集合 13 */ 14 public List<Student> list(); 15 16 /** 17 * 根据学生id获取对应的学生对象 18 * @param id 19 * @return 20 */ 21 public Student get(int id); 22 23 /** 24 * 根据指定id删除学生信息 25 * @param id 学生id 26 * @return 删除成功返回true,失败返回false 27 */ 28 public boolean delete(int id); 29 30 /** 31 * 根据传入学生对象的信息,修改学生 32 * 如果传入学生对象的id能够找到对象的信息,那么就修改 33 * @param student 学生对象,将需要修改的信息封装在这个对象中 34 * @return 修改成功true,否则false 35 */ 36 public boolean update(Student student); 37 38 39 /** 40 * 传入学生对象,加入到文本信息中 41 * @param student 学生对象 42 * @return 添加成功true,否则false 43 */ 44 public boolean add(Student student); 45 46 47 }
3、学生管理类接口的实现类/MyStudentManagement
1 package com.yj.student; 2 3 import java.io.BufferedReader; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 import java.nio.file.Path; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 public class StudentManagerImpl implements IStudentManager { 14 15 /** 自定义文本位置的路径 */ 16 private String Path; 17 18 /** 19 * 默认构造方法,给到了默认路径 20 */ 21 public StudentManagerImpl() { 22 this.Path = DEFAULT_PATH; 23 } 24 25 /** 26 * 根据传入的路径读取文本文件 27 * 28 * @param path 传入的文件路径 29 */ 30 public StudentManagerImpl(String path) { 31 this.Path = path; 32 } 33 34 @Override 35 public List<Student> list() { 36 List<Student> list = new ArrayList<Student>(); 37 BufferedReader br = null; 38 39 try { 40 br = new BufferedReader(new FileReader(this.Path)); 41 String str = null; 42 while ((str = br.readLine()) != null) { 43 String[] strs = str.split("\\s+"); 44 int id = Integer.parseInt(strs[0]); 45 String name = strs[1]; 46 int age = Integer.parseInt(strs[2]); 47 48 Student s = new Student(id, name, age); 49 list.add(s); 50 } 51 } catch (FileNotFoundException e) { 52 e.printStackTrace(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } finally { 56 try { 57 if (br != null) { 58 br.close(); 59 } 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 return list; 65 } 66 67 @Override 68 public Student get(int id) { 69 Student stu = null; 70 BufferedReader br = null; 71 72 try { 73 br = new BufferedReader(new FileReader(this.Path)); 74 String str = null; 75 while ((str = br.readLine()) != null) { 76 String[] strs = str.split("\\s+"); 77 int sid = Integer.parseInt(strs[0]); 78 String name = strs[1]; 79 int age = Integer.parseInt(strs[2]); 80 if (id == sid) { 81 stu = new Student(sid, name, age); 82 break; 83 } 84 85 } 86 } catch (FileNotFoundException e) { 87 e.printStackTrace(); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } finally { 91 try { 92 if (br != null) { 93 br.close(); 94 } 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 } 99 return stu; 100 } 101 102 @Override 103 public boolean delete(int id) { 104 //先把文本文档中的内容读取到内存中 105 List<Student> stus = list(); 106 boolean flag = false; 107 108 // 在集合中找到对象并且删除对应的学生对象 109 for (int i = 0; i < stus.size(); i++) { 110 Student s = stus.get(i); 111 if (s.getId() == id) { 112 flag = true; 113 stus.remove(s); 114 break; 115 } 116 } 117 // 将集合中的内容整个重新写到文本文件中 118 if (flag) { 119 PrintWriter out = null; 120 try { 121 out = new PrintWriter(Path); 122 for (int i = 0; i < stus.size(); i++) { 123 Student s = stus.get(i); 124 String str = s.getId()+" "+s.getName()+" "+s.getAge(); 125 out.println(str); 126 } 127 128 } catch (FileNotFoundException e) { 129 e.printStackTrace(); 130 } finally { 131 try { 132 out.close(); 133 } catch (Exception e) { 134 flag = false; 135 e.printStackTrace(); 136 } 137 } 138 } 139 return flag; 140 } 141 142 @Override 143 public boolean update(Student student) { 144 // 先把文本文档中的内容读取到内存中 145 List<Student> stus = list(); 146 boolean flag = false; 147 148 // 在集合中找到对象并且删除对应的学生对象 149 for (int i = 0; i < stus.size(); i++) { 150 Student s = stus.get(i); 151 if (s.getId() == student.getAge()) { 152 flag = true; 153 s.setAge(student.getAge()); 154 s.setName(student.getName()); 155 break; 156 } 157 } 158 // 将集合中的内容整个重新写到文本文件中 159 if (flag) { 160 PrintWriter out = null; 161 try { 162 out = new PrintWriter(Path); 163 for (int i = 0; i < stus.size(); i++) { 164 Student s = stus.get(i); 165 String str = s.getId() + " " + s.getName() + " " + s.getAge(); 166 out.println(str); 167 } 168 169 } catch (FileNotFoundException e) { 170 flag = false; 171 e.printStackTrace(); 172 } finally { 173 try { 174 out.close(); 175 } catch (Exception e) { 176 flag = false; 177 e.printStackTrace(); 178 } 179 } 180 } 181 return flag; 182 } 183 184 @Override 185 public boolean add(Student student) { 186 // 先把文本文档中的内容读取到内存中 187 boolean flag = false; 188 PrintWriter out = null; 189 190 try { 191 out = new PrintWriter(new FileWriter(this.Path, true)); 192 out.println(student.getId() + " " + student.getName() + " " + student.getAge()); 193 flag = true; 194 } catch (FileNotFoundException e) { 195 e.printStackTrace(); 196 } catch (IOException e) { 197 e.printStackTrace(); 198 } finally { 199 try { 200 out.close(); 201 } catch (Exception e2) { 202 e2.printStackTrace(); 203 } 204 205 } 206 return flag; 207 } 208 209 public String getPath() { 210 return Path; 211 } 212 213 public void setPath(String path) { 214 this.Path = path; 215 } 216 217 }
/** 学生id */