1. 复制上例中的Student类到本项目中;
    2. 在项目目录下创建文件name.txt,并写入以下内容:
      it001,武松,男,22
      it002,林冲,男,20
      it003,卢俊义,男,18
      it004,石宝,男,23
    3. 创建MainApp类,包含main()方法
    4. 在main()方法中:
    5. 定义一个存储Student对象的集合;
    6. 循环从文件中读取数据,每次读取一行,并解析每个属性值,封装到一个Student对象中,并将这个对象封装到集合中。
    7. 遍历集合,找到要修改的“卢俊义”,并将姓名修改为:“史文恭”。
      同时将数据再次写回到文件中。
    8. 程序结束后,验证文件中的数据是否修改成功。 ```java package Day10.Demo03;/*

      @create 2020—11—26—18:26 */

    public class Student { private String id; private String name; private String sex; private String age;

    1. public Student() {
    2. }
    3. public Student(String id, String name, String sex, String age) {
    4. this.id = id;
    5. this.name = name;
    6. this.sex = sex;
    7. this.age = age;
    8. }
    9. public String getId() {
    10. return id;
    11. }
    12. public void setId(String id) {
    13. this.id = id;
    14. }
    15. public String getName() {
    16. return name;
    17. }
    18. public void setName(String name) {
    19. this.name = name;
    20. }
    21. public String getSex() {
    22. return sex;
    23. }
    24. public void setSex(String sex) {
    25. this.sex = sex;
    26. }
    27. public String getAge() {
    28. return age;
    29. }
    30. public void setAge(String age) {
    31. this.age = age;
    32. }
    33. @Override
    34. public String toString() {
    35. return "Student{" +
    36. "id='" + id + '\'' +
    37. ", name='" + name + '\'' +
    38. ", sex='" + sex + '\'' +
    39. ", age='" + age + '\'' +
    40. '}';
    41. }

    }

    1. ```java
    2. package Day10.Demo03;/*
    3. @create 2020--11--26--19:05
    4. */
    5. import java.io.BufferedReader;
    6. import java.io.BufferedWriter;
    7. import java.io.FileReader;
    8. import java.io.FileWriter;
    9. import java.util.ArrayList;
    10. import java.util.Arrays;
    11. public class MainApp {
    12. public static void main(String[] args) throws Exception{
    13. BufferedWriter f = new BufferedWriter(new FileWriter("name.txt"));
    14. f.write("it001,武松,男,22\n");
    15. f.write("it002,林冲,男,20\n");
    16. f.write("it003,卢俊义,男,18\n");
    17. f.write("it004,石宝,男,23\n");
    18. f.close();
    19. BufferedReader br = new BufferedReader(new FileReader("name.txt"));
    20. ArrayList<String> arr = new ArrayList<>();
    21. char[] chs = new char[14];
    22. int len;
    23. while ((len = br.read(chs)) != -1) {
    24. arr.add(new String(chs));
    25. System.out.println(new String(chs,0,len));
    26. }
    27. System.out.println(arr.get(0));
    28. ArrayList<Student> students = new ArrayList<>();
    29. for (int i = 0; i < arr.size()-1; i++) {
    30. String[] s = String.valueOf(arr.get(i)).replace("\n","").split(",");
    31. System.out.println(Arrays.toString(s));
    32. students.add(new Student(s[0], s[1], s[2], s[3]));
    33. }
    34. for (Student student : students) {
    35. if ("卢俊义".equals(student.getName())) {
    36. student.setName("史文恭");
    37. }
    38. }
    39. for (Student student : students) {
    40. System.out.println(student);
    41. }
    42. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("name.txt"));
    43. for (Student student : students) {
    44. String s = student.getId()+","+student.getName()+","+student.getSex()+","+student.getAge()+"\n";
    45. bufferedWriter.write(s);
    46. }
    47. bufferedWriter.close();
    48. br.close();
    49. }
    50. }