Java 基础 45 字符流相关练习

1.1 字符流的练习之5种方式复制文本文件

1.1.1 字符流复制文本文件的五种方式

  • 基本字符流一次读写一个字符
  • 基本字符流一次读写一个字符数组
  • 缓冲字符流一次读写一个字符
  • 缓冲字符流一次读写一个字符数组
  • 缓冲字符串一次读写一个字符串

1.1.2 案例代码

public class CopyFileTest {
    public static void main(String[] args) throws IOException {
        method1();
        // method2();
        // method3();
        // method4();
        // method5();
    }
    
    //缓冲字符流一次读写一个字符串
    private static void method5() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        String line;
        while((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        
        bw.close();
        br.close();
    }
    
    //缓冲字符流一次读写一个字符数组
    private static void method4() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        char[] chs = new char[1024];
        int len;
        while((len=br.read(chs))!=-1) {
            bw.write(chs, 0, len);
        }
        
        bw.close();
        br.close();
    }
    
    //缓冲字符流一次读写一个字符
    private static void method3() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\林青霞.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("窗里窗外.txt"));
        
        int ch;
        while((ch=br.read())!=-1) {
            bw.write(ch);
        }
        
        bw.close();
        br.close();
    }
    
    // 基本字符流一次读写一个字符数组
    private static void method2() throws IOException {
        FileReader fr = new FileReader("d:\\林青霞.txt");
        FileWriter fw = new FileWriter("窗里窗外.txt");

        char[] chs = new char[1024];
        int len;
        while((len=fr.read(chs))!=-1) {
            fw.write(chs, 0, len);
        }

        fw.close();
        fr.close();
    }

    // 基本字符流一次读写一个字符
    private static void method1() throws IOException {
        FileReader fr = new FileReader("d:\\林青霞.txt");
        FileWriter fw = new FileWriter("窗里窗外.txt");

        int ch;
        while ((ch = fr.read()) != -1) {
            fw.write(ch);
        }

        fw.close();
        fr.close();
    }
}

1.2 字符流的练习之把集合中的字符串数据存储到文本文件

1.2.1 案例分析

  • 创建集合对象
  • 往集合中添加字符串元素
  • 创建字符缓冲输出流对象
  • 遍历集合,得到每一个字符串元素,把字符串元素作为数据写入到文本文件
  • 释放资源

1.2.2 案例代码


public class ArrayListToFileTest {
    public static void main(String[] args) throws IOException {
        //创建集合对象
        ArrayList<String> array = new ArrayList<String>();
        
        //往集合中添加字符串元素
        array.add("hello");
        array.add("world");
        array.add("java");
        
        //创建字符缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));
        
        //遍历集合,得到每一个字符串元素,把字符串元素作为数据写入到文本文件
        for(String s : array) {
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        
        //释放资源
        bw.close();
    }
}

1.3 字符流的练习之把文本文件中的字符串数据读取到集合

1.3.1 案例分析

需求:

  从文本文件中读取数据到ArrayList集合中,并遍历集合
每一行数据作为一个字符串元素

分析:

  • 创建字符缓冲输入流对象
  • 创建集合对象
  • 读取数据,每一次读取一行,并把该数据作为元素存储到集合中
  • 释放资源
  • 遍历集合

1.3.2 案例代码

public class FileToArrayListTest {
    public static void main(String[] args) throws IOException {
        //创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("array.txt"));
        
        //创建集合对象
        ArrayList<String> array = new ArrayList<String>();
        
        //读取数据,每一次读取一行,并把该数据作为元素存储到集合中
        String line;
        while((line=br.readLine())!=null) {
            array.add(line);
        }
        
        //释放资源
        br.close();
        
        //遍历集合
        for(String s : array) {
            System.out.println(s);
        }
    }
}

1.4 字符流的练习之把集合中的学生对象数据存储到文本文件

1.4.1案例分析

需求:

  • 把ArrayList集合中的学生数据存储到文本文件
  • 每一个学生数据作为文件中的一行数据
学号 姓名 年龄 所在城市
it001 张曼玉 35 北京
it002 王祖贤 33 上海
it003 林青霞 30 西安

定义一个学生类

学号 姓名 年龄 所在城市
it001 张曼玉 35 北京
it002 王祖贤 33 上海
it003 林青霞 30 西安

分析:

  • 创建集合对象
  • 创建学生对象
  • 把学生对象添加到集合中
  • 创建字符缓冲输出流对象
  • 遍历集合,得到每一个学生对象,然后把该对象的数据拼接成一个指定格式的字符串写到文本文件
  • 释放资源

1.4.2 案例代码

public class ArrayListToFileTest {
    public static void main(String[] args) throws IOException {
        // 创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        // 创建学生对象
        Student s1 = new Student("it001", "张曼玉", 35, "北京");
        Student s2 = new Student("it002", "王祖贤", 33, "上海");
        Student s3 = new Student("it003", "林青霞", 30, "西安");

        // 把学生对象添加到集合中
        array.add(s1);
        array.add(s2);
        array.add(s3);

        // 创建字符缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));

        // 遍历集合,得到每一个学生对象,然后把该对象的数据拼接成一个指定格式的字符串写到文本文件
        for (Student s : array) {
            // it001,张曼玉,35,北京
            StringBuilder sb = new StringBuilder();
            sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",")
                    .append(s.getCity());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        
        //释放资源
        bw.close();
    }
}

1.5 字符流的练习之把文本文件中的学生对象数据读取到集合

1.5.1 案例分析

需求:
- 从文本文件中读取学生数据到ArrayList集合中,并遍历集合
- 每一行数据作为一个学生元素

    it001,张曼玉,35,北京

这里我们要使用String类中的一个方法:split()

分析:

  • 创建字符缓冲输入流对象
  • 创建集合对象
  • 读取数据,每一次读取一行数据,把该行数据想办法封装成学生对象,并把学生对象存储到集合中
  • 释放资源
  • 遍历集合

1.5.2 案例代码

public class FileToArrayListTest {
    public static void main(String[] args) throws IOException {
        // 创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("students.txt"));

        // 创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        // 读取数据,每一次读取一行数据,把该行数据想办法封装成学生对象,并把学生对象存储到集合中
        String line;
        while ((line = br.readLine()) != null) {
            // it001,张曼玉,35,北京
            String[] strArray = line.split(",");

            Student s = new Student();
            s.setSid(strArray[0]);
            s.setName(strArray[1]);
            s.setAge(Integer.parseInt(strArray[2]));
            s.setCity(strArray[3]);

            array.add(s);
        }

        // 释放资源
        br.close();

        // 遍历集合
        for (Student s : array) {
            System.out.println(s.getSid() + "---" + s.getName() + "---" + s.getAge() + "---" + s.getCity());
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容