Day 61

_集合到文件

  1. 需求:把ArrayList集合中的字符串数据写入文本文件。要求:每一个字符串元素作为文件中的一行数据

  2. 思路:

    • 创建ArrayList集合
    • 往集合中存储字符串元素
    • 创建字符缓冲输出流对象
    • 遍历集合,得到每一个字符串数据
    • 调用字符缓冲输出流对象的方法写数据
    • 释放资源
  3. package demo15;
    
    import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class IO_Demo_01 {
        public static void main(String[] args) {
            /*
            需求:把ArrayList集合中的字符串数据写入文本文件。要求:每一个字符串元素作为文件中的一行数据
    
            思路:
                - 创建ArrayList集合
                - 往集合中存储字符串元素
                - 创建字符缓冲输出流对象
                - 遍历集合,得到每一个字符串数据
             */
    
            //  创建ArrayList集合
            ArrayList<String> arr = new ArrayList<>();
    
            // 向arr添加元素
            arr.add("hello");
            arr.add("world");
            arr.add("java");
    
            // 创建字符缓冲流输出对象
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo15\\note_01.txt"));
                // 遍历集合
                for (String s : arr) {
                    bw.write(s);
                    bw.newLine();
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    ========================================
    
    Process finished with exit code 0
    ****************************************
    note_01
    hello
    world
    java
    
    

_文件到集合

  1. 需求:把文本文件中的数据读取到集合中,并遍历集合。要求:文件中的每一行数据是一个集合元素。

  2. 思路:

    • 创建字符缓冲输入流对象
    • 创建ArrayList集合对象
    • 调用字符缓冲流输入对象的方法读取数据
    • 把读取到的字符串数据存储到集合中
    • 释放资源
    • 遍历集合
  3. package demo15;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class IO_Demo_02 {
        public static void main(String[] args) {
            /*
            需求:把文本文件中的数据读取到集合中,并遍历集合。要求:文件中的每一行数据是一个集合元素
    
            思路:
                - 创建字符缓冲输入流对象
                - 创建ArrayList集合对象
                - 调用字符缓冲流输入对象的方法读取数据
                - 把读取到的字符串数据存储到集合中
                - 释放资源
                - 遍历集合
             */
    
            ArrayList<String> arr = new ArrayList<>();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo15\\note_01.txt"));
                if (br != null) {
                    String len;
                    while ((len = br.readLine()) != null) {
                        arr.add(len);
                    }
    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            // 遍历集合
            for (String s : arr) {
                System.out.println(s);
            }
        }
    }
    ======================================
    hello
    world
    java
    
    Process finished with exit code 0
    

_点名器

  1. 需求:以文件里面存储了班级同学的姓名,每个姓名占一行,要求通过程序实现随机点名器

  2. 思路:

    • 创建字符缓冲输入流对象
    • 创建ArrayList集合对象
    • 调用字符缓冲输入流对象的方法读数据
    • 把读取到的字符串数据存储到集合中
    • 释放资源
    • 使用Random产生一个随机数,随机数的范围在:[0,集合的长度]
    • 把第六步产生的随机数作为索引到ArrayList集合中获取值
    • 把第七步得到的数据输出在控制台
  3. package demo15;
    
    import java.io.*;
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class IO_Demo_03 {
        public static void main(String[] args) {
            /*
            需求:以文件里面存储了班级同学的姓名,每个姓名占一行,要求通过程序实现随机点名器
    
            思路:
                - 创建字符缓冲输入流对象
                - 创建ArrayList集合对象
                - 调用字符缓冲输入流对象的方法读数据
                - 把读取到的字符串数据存储到集合中
                - 释放资源
                - 使用Random产生一个随机数,随机数的范围在:[0,集合的长度]
                - 把第六步产生的随机数作为索引到ArrayList集合中获取值
                - 把第七步得到的数据输出在控制台
             */
            ArrayList<String> arr = new ArrayList<>();
            BufferedReader br = null;
            // - 调用字符缓冲输入流对象的方法读数据
            try {
                br = new BufferedReader(new FileReader("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo15\\name.txt"));
                if (br != null) {
                    String len;
                    while ((len = br.readLine()) != null) {
                        arr.add(len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 生成一个随机数
            Random random = new Random();
            /*
            public int nextInt(int bound)
            返回伪随机的,均匀分布int值介于0(含)和指定值(不包括),从该随机数生成器的序列绘制。
             nextInt的一般合同是指定范围内的int值被伪随机生成并返回。
             所有bound可能的int值以(近似)等概率产生
             */
            int code = random.nextInt(arr.size());
            // 把第六步产生的随机数作为索引到ArrayList集合中获取值
    
            System.out.println("幸运者是:"+arr.get(code));
    
        }
    }
    ====================================
    幸运者是:小天
    
    Process finished with exit code 0
    ************************************
    name.txt
    小明
    小红
    小军
    晓军
    小天
    小白
    晓丽
    老王
    老李
    老刘
    

_集合到文件改进版

  1. 需求:把ArrayList集合中的学生数据写入到文本文件中。要求:每一个学生对象的数据作为文件中的一行数据

    • 格式:学号,姓名,年龄,居住地
  2. 思路:

    • 定义学生类
    • 创建ArrayList集合
    • 创建学生对象
    • 把学生对象添加到集合中
    • 创建字符缓冲输出流对象
    • 遍历集合,得到每个学生对象
    • 把学生对象的数据拼接成指定格式的字符串
    • 调用字符缓冲输出流对象的方法写数据
    • 释放资源
  3. package demo15;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class _IO_Demo_04 {
        public static void main(String[] args) {
            /*
            需求:把ArrayList集合中的学生数据写入到文本文件中。要求:每一个学生对象的数据作为文件中的一行数据
                 格式:学号,姓名,年龄,居住地
    
            思路:
                - 定义学生类
                - 创建ArrayList集合
                - 创建学生对象
                - 把学生对象添加到集合中
                - 创建字符缓冲输出流对象
                - 遍历集合,得到每个学生对象
                - 把学生对象的数据拼接成指定格式的字符串
                - 调用字符缓冲输出流对象的方法写数据
                - 释放资源
             */
    
            ArrayList<Student> arr_01 = new ArrayList<>();
    
            // 创建学生对象
            Student s1 =new Student(100, "小红", 18, "广东");
            Student s2 =new Student(102, "小明", 20, "北京");
            Student s3 =new Student(103, "小军", 21, "上海");
            Student s4 =new Student(104, "老王", 45, "啊Marry卡");
    
            // 将学生对象添加到集合中
            arr_01.add(s1);
            arr_01.add(s2);
            arr_01.add(s3);
            arr_01.add(s4);
    
            // 创建arr_02 集合,用于存放包装成String类型的学生对象数据
            ArrayList<String> arr_02 = new ArrayList<>();
    
            // 遍历arr_01集合,得到每个学生对象,然后封装成String类型存入arr_02中
            for (Student s : arr_01) {
                StringBuilder stringBuilder = new StringBuilder();
                StringBuilder value = stringBuilder.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
                arr_02.add(value.toString());
            }
    
            /*
            // 遍历集合
            for (String s: arr_02) {
                System.out.println(s);
            }
    
             */
    
            // 创建字符输出流对象,遍历arr_02对象,将arr_02中的元素进行IO流输出
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo15\\student.txt"));
                String len;
                // 遍历集合arr_02
                for (String s : arr_02) {
                    bw.write(s);
                    bw.newLine();
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    -----------------------------------------------
    package demo15;
    
    public class Student {
        public Student(){
        }
        private int Id;
        private String name;
        private int age;
        private String address;
    
        public Student(int Id, String name, int age, String address) {
            this.Id = Id;
            this.name = name;
            this.age = age;
            this.address = address;
        }
        public int getId() {
            return Id;
        }
    
        public void setId(int id) {
            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 getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    =========================================
    
    Process finished with exit code 0
    *****************************************
    student.txt
    100,小红,18,广东
    102,小明,20,北京
    103,小军,21,上海
    104,老王,45,Marry
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值