JavaSE-18.3.3【IO流练习案例-集合到文件(改进版)、文件到集合(改进版)】

 1 package day9.lesson3;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 
 8 /*
 9 3.4 案例-集合到文件(改进版)
10 
11     把ArrayList集合中的学生数据写入到文本文件。要求:每一个学生对象的数据作为文件中的一行数据
12     格式:学号,姓名,年龄,居住地
13     举例:itheima001,林青霞,30,西安
14 
15     定义学生类
16     创建ArrayList集合
17     创建学生对象
18     把学生对象添加到集合中
19     创建字符缓冲输出流对象
20     遍历集合,得到每一个学生对象
21     把学生对象的数据拼接成指定格式的字符串
22     调用字符缓冲输出流对象的方法写数据
23     释放资源
24  */
25 public class ArrayListToTxtDemo2 {
26     public static void main(String[] args) throws IOException {
27         ArrayList<Student> arrayList = new ArrayList<>();
28 
29         Student s1 = new Student(1,"张三", 20, "南京");
30         Student s2 = new Student(2,"李四", 23, "杭州");
31         Student s3 = new Student(3,"王五", 21, "武汉");
32 
33         arrayList.add(s1);
34         arrayList.add(s2);
35         arrayList.add(s3);
36 
37         BufferedWriter bw = new BufferedWriter(new FileWriter("stage2\\src\\day9\\lesson3\\students.txt"));
38 
39         for (Student stu: arrayList){
40             /*bw.write(stu.getSid() + "," + stu.getName() + "," + stu.getAge() + "," + stu.getAddress());
41             bw.newLine();
42             bw.flush();*/
43 
44             StringBuilder sb = new StringBuilder();
45             sb.append(stu.getSid()).append(",").append(stu.getName()).append(",").append(stu.getAge()).append(",").append(stu.getAddress());
46             bw.write(sb.toString());
47             bw.newLine();
48             bw.flush();
49         }
50 
51         bw.close();
52     }
53 }
 1 package day9.lesson3;
 2 
 3 public class Student {
 4 
 5     private int sid;
 6     private String name;
 7     private int age;
 8     private String address;
 9 
10     public Student() {
11     }
12 
13     public Student(int sid, String name, int age, String address) {
14         this.sid = sid;
15         this.name = name;
16         this.age = age;
17         this.address = address;
18     }
19 
20     public void setSid(int sid) {
21         this.sid = sid;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public void setAge(int age) {
29         this.age = age;
30     }
31 
32     public void setAddress(String address) {
33         this.address = address;
34     }
35 
36     public int getSid() {
37         return sid;
38     }
39 
40     public String getName() {
41         return name;
42     }
43 
44     public int getAge() {
45         return age;
46     }
47 
48     public String getAddress() {
49         return address;
50     }
51 }
 1 package day9.lesson3;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 
 8 /*
 9 3.5 案例-文件到集合(改进版)
10 
11     把文本文件中的数据读取到集合中,并遍历集合。
12     要求:文件中每一行数据是一个学生对象的成员变量值
13     举例:itheima001,林青霞,30,西安
14 
15     定义学生类
16     创建字符缓冲输入流对象
17     创建ArrayList集合对象
18     调用字符缓冲输入流对象的方法读数据
19     把读取到的字符串数据用split()进行分割,得到一个字符串数组
20     创建学生对象
21     把字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
22     把学生对象添加到集合
23     释放资源
24     遍历集合
25  */
26 public class TxtToArrayListDemo2 {
27     public static void main(String[] args) throws IOException {
28         BufferedReader br = new BufferedReader(new FileReader("stage2\\src\\day9\\lesson3\\students.txt"));
29 
30         ArrayList<Student> arrayList = new ArrayList<>();
31 
32         String line;
33         while ((line=br.readLine()) != null){
34             String[] strArr = line.split(",");
35             Student student = new Student();
36 
37             student.setSid(Integer.parseInt(strArr[0])); //str转int
38             student.setName(strArr[1]);
39             student.setAge(Integer.parseInt(strArr[2]));
40             student.setAddress(strArr[3]);
41 
42             arrayList.add(student);
43         }
44 
45         br.close();
46 
47         for (Student s: arrayList){
48             System.out.println(s.getSid() + "," + s.getName() + "," + s.getAge() + "," + s.getAddress());
49         }
50     }
51 }

 

posted @ 2021-06-07 11:26  yub4by  阅读(69)  评论(0编辑  收藏  举报