fanjialong 3 godzin temu
rodzic
commit
b54745124e

+ 11 - 0
java-base-project10/day22/day22.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 19 - 0
java-base-project10/day22/src/com/sf/_01_encoding/Test.java

@@ -0,0 +1,19 @@
+package com.sf._01_encoding;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+
+public class Test {
+    public static void main(String[] args) throws UnsupportedEncodingException {
+        /**
+         * 编码: str.getBytes(编码规则);   把字符串转成字节数组  十进制字节数组
+         * 解码: new String(byte[] , 解码规则)
+         */
+        String str = "abac中";
+        byte[] bytes = str.getBytes("UTF-8");
+        System.out.println(Arrays.toString(bytes));
+
+        String str1 = new String(bytes,"GBK");
+        System.out.println(str1);
+    }
+}

+ 86 - 0
java-base-project10/day22/src/com/sf/_02_字节流输出流/Student.java

@@ -0,0 +1,86 @@
+package com.sf._02_字节流输出流;
+
+public class Student {
+    private Long id;
+    private String name;
+    private Integer age;
+    private Double score;
+
+    public Student() {
+    }
+
+    public Student(Long id, String name, Integer age, Double score) {
+        this.id = id;
+        this.name = name;
+        this.age = age;
+        this.score = score;
+    }
+
+    /**
+     * 获取
+     * @return id
+     */
+    public Long getId() {
+        return id;
+    }
+
+    /**
+     * 设置
+     * @param id
+     */
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public Integer getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return score
+     */
+    public Double getScore() {
+        return score;
+    }
+
+    /**
+     * 设置
+     * @param score
+     */
+    public void setScore(Double score) {
+        this.score = score;
+    }
+
+    public String toString() {
+        return "Student{id = " + id + ", name = " + name + ", age = " + age + ", score = " + score + "}";
+    }
+}

+ 39 - 0
java-base-project10/day22/src/com/sf/_02_字节流输出流/Test.java

@@ -0,0 +1,39 @@
+package com.sf._02_字节流输出流;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class Test {
+    /**
+     * 字节输出流: FileOutputStream
+     * 操作1 创建一个字节输出流
+     *    2 往文件当中写内容write(单个字节), write(字节数组)
+     *    3 关闭资源
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        // 创建文件
+        File file = new File("D:\\a.txt");
+        // 创建字节输出流
+        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
+        /**
+         * 1 现在在去写内容的时候只能够一个一个字节去写 , 这样非常不方便我们也记不住这么多字节对应哪些内容
+         * 2 写的时候不换行
+         * 3 写的时候不追加
+         */
+//        fileOutputStream.write(98);
+//        fileOutputStream.write(99);
+//        fileOutputStream.write(100);
+        String str = "fanjialong";
+        fileOutputStream.write(str.getBytes());
+
+        // 在文件写的时候需要写一个换行符 windows 和mac 换行符是不一样
+//        fileOutputStream.write(System.lineSeparator().getBytes());
+//
+//        String str1 = "fanjialong";
+//        fileOutputStream.write(str1.getBytes());
+        // 关闭资源
+        fileOutputStream.close();
+    }
+}

+ 52 - 0
java-base-project10/day22/src/com/sf/_02_字节流输出流/Test1.java

@@ -0,0 +1,52 @@
+package com.sf._02_字节流输出流;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Scanner;
+
+public class Test1 {
+    /**
+     * 要求在控制台当中录入你的
+     * 姓名: fanjialong
+     * 年龄: xx
+     * 身高: xx
+     * 体重: xx
+     * 注意: 每录入完信息就要信息追加到文件中
+     */
+    public static void main(String[] args) throws IOException {
+        // 创建文件删除流写内容
+        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\a.txt"),true);
+
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入你的姓名");
+        String name = scanner.next();
+        // 写入姓名
+        fileOutputStream.write(("姓名:"+name).getBytes());
+        // 写换行符
+        fileOutputStream.write(System.lineSeparator().getBytes());
+
+        System.out.println("请录入你的年龄");
+        String age = scanner.next();
+        // 写入姓名
+        fileOutputStream.write(("年龄:"+age).getBytes());
+        // 写换行符
+        fileOutputStream.write(System.lineSeparator().getBytes());
+
+        System.out.println("请录入你的身高");
+        String height = scanner.next();
+        // 写入姓名
+        fileOutputStream.write(("身高:"+height).getBytes());
+        // 写换行符
+        fileOutputStream.write(System.lineSeparator().getBytes());
+
+        System.out.println("请录入你的体重");
+        String weight = scanner.next();
+        // 写入姓名
+        fileOutputStream.write(("体重:"+weight).getBytes());
+        // 写换行符
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        // 关闭资源
+        fileOutputStream.close();
+    }
+}

+ 83 - 0
java-base-project10/day22/src/com/sf/_02_字节流输出流/Test2.java

@@ -0,0 +1,83 @@
+package com.sf._02_字节流输出流;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class Test2 {
+    /**
+     * 创建Student stu(id,name,age,score)
+     * 创建出来一个List<Student> list -> 添加3个学生分数要是一个无需
+     *
+     * 把集合信息最终写到student.txt 文件中
+     *
+     * 分数要从大到小降序排序
+     * id    name    age   score    表头
+     * 1    zhangsan 10    99
+     * 2     lisi    20    98
+     * 3     wangwu  10    90
+     *
+     * 最大分数:   99
+     * 最小分数;   90
+     * 平均分数:   xxx
+     * 总分:   xxxx
+     * 总人数: xxx
+     */
+    public static void main(String[] args) throws IOException {
+        // 先把学生放到集合中
+        List<Student> list = new ArrayList<>();
+        list.add(new Student(1L,"zhangsan",15,90.0));
+        list.add(new Student(2L,"lisi",16,80.0));
+        list.add(new Student(3L,"wangwu",14,99.0));
+        // 堆集合进行排序
+        Collections.sort(list, new Comparator<Student>() {
+            @Override
+            public int compare(Student o1, Student o2) {
+                return (int) (o2.getScore()-o1.getScore());
+            }
+        });
+        System.out.println(list);
+        // 求出来最大值
+        Student maxStudent = list.get(0);
+        // 求最小值
+        Student minStudent = list.get(list.size()-1);
+        // 求总人数
+        int totalStudents = list.size();
+        // 总分
+        double totalScore = 0;
+        for (Student student : list) {
+            totalScore += student.getScore();
+        }
+        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\student.txt"),true);
+        fileOutputStream.write("学生信息列表".getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+
+        // 写表头
+        String headStr = "id       name        age       score";
+        fileOutputStream.write(headStr.getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        //写表格内容
+        for (Student student : list) {
+            fileOutputStream.write((student.getId()+"       "+student.getName()+"        "+student.getAge()+"       "+student.getScore()).getBytes());
+            fileOutputStream.write(System.lineSeparator().getBytes());
+        }
+        //写其他信息
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        fileOutputStream.write(("最大分数:"+ maxStudent.getScore()).getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        fileOutputStream.write(("最小分数:"+ minStudent.getScore()).getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        fileOutputStream.write(("平均分:"+ totalScore/list.size()).getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        fileOutputStream.write(("总分:"+ totalScore).getBytes());
+        fileOutputStream.write(System.lineSeparator().getBytes());
+        fileOutputStream.write(("总人数:"+ list.size()).getBytes());
+
+
+
+    }
+}

+ 45 - 0
java-base-project10/day22/src/com/sf/_02_字节流输出流/Test3.java

@@ -0,0 +1,45 @@
+package com.sf._02_字节流输出流;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Scanner;
+
+public class Test3 {
+
+    /**
+     * 做一个登录日志记录
+     * 通过控制台录入账号和密码
+     * 如果账号为admin  密码为123 就表示登录成功 如果不是表示登录失败
+     *
+     * 关键点:  登录成功和登录失败都需要往 login.txt 插入日志信息
+     * 日志格式
+     * zhangsan -  时间(2026年1月19日 11:40)- 登录失败
+     * admin -  时间(2026年1月19日 11:41)- 登录成功
+     */
+    public static void main(String[] args) throws IOException {
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请录入账号");
+        String username = scanner.next();
+        System.out.println("请录入密码");
+        String password = scanner.next();
+
+        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\login.txt"),true);
+        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
+        String dateStr = sdf.format(new Date());
+
+        if("admin".equals(username)&& "123".equals(password)){
+            System.out.println("登录成功");
+            //写入登录成功日志
+            fileOutputStream.write((username+"  - "+dateStr  + "登录成功").getBytes());
+        }else{
+            System.out.println("登录失败");
+            //写入登录失败日志
+            fileOutputStream.write((username+"  - "+dateStr  + "登录失败").getBytes());
+        }
+        //关闭资源
+        fileOutputStream.close();
+    }
+}

+ 69 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Student.java

@@ -0,0 +1,69 @@
+package com.sf._03_字节输入流;
+
+public class Student {
+    private String name;
+    private Integer age;
+    private String email;
+
+
+    public Student() {
+    }
+
+    public Student(String name, Integer age, String email) {
+        this.name = name;
+        this.age = age;
+        this.email = email;
+    }
+
+    /**
+     * 获取
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * 设置
+     * @param name
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 获取
+     * @return age
+     */
+    public Integer getAge() {
+        return age;
+    }
+
+    /**
+     * 设置
+     * @param age
+     */
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    /**
+     * 获取
+     * @return email
+     */
+    public String getEmail() {
+        return email;
+    }
+
+    /**
+     * 设置
+     * @param email
+     */
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
+    public String toString() {
+        return "Student{name = " + name + ", age = " + age + ", email = " + email + "}";
+    }
+}

+ 70 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test.java

@@ -0,0 +1,70 @@
+package com.sf._03_字节输入流;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+public class Test {
+    /**
+     * 字节输入流:
+     * 把磁盘当中文件内容读取到程序中
+     *
+     * 1 创建字节输入流
+     * 2 read(字节)  read(字节数组)
+     * 3 关闭资源
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        // 1 创建出来字节输入流
+        FileInputStream fileInputStream = new FileInputStream(new File("D:\\a.txt"));
+        // 2 读取内容
+        /**
+         * 每次调用read 都可以读取一个字节
+         * 如果读取不到了就是-1
+         */
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+//        System.out.println(fileInputStream.read());
+
+        byte[] buffer = new byte[2];
+        /**
+         * 单纯就调用read()  返回值就是你读取那个字节内容
+         * 比如你读取a 字节返回值  97   你读去b 返回就是98
+         *
+         * 如果你调用read(byte[])  返回值你读取字节长度
+         */
+        // a b
+        System.out.println(fileInputStream.read(buffer)); //2
+        System.out.println(Arrays.toString(buffer));
+        System.out.println(new String(buffer));
+        //c e
+        System.out.println(fileInputStream.read(buffer)); //2
+        System.out.println(Arrays.toString(buffer));
+        System.out.println(new String(buffer));
+        //d a
+        System.out.println(fileInputStream.read(buffer)); //2
+        System.out.println(Arrays.toString(buffer));
+        System.out.println(new String(buffer));
+        // a
+        int readLen = fileInputStream.read(buffer);
+        // readLen = 1
+        System.out.println(fileInputStream.read(buffer)); //1
+        // [97,97]  我们读取真实长度其实就只有前面这个97  后面97 是他jdk 给填充的是无效的
+        System.out.println(Arrays.toString(buffer));
+        System.out.println(new String(buffer,0,readLen));
+        //
+        System.out.println(fileInputStream.read(buffer)); //-1
+        // [97,97]
+        System.out.println(Arrays.toString(buffer));
+        System.out.println(new String(buffer,0,readLen));
+
+        // 3 关闭资源
+        fileInputStream.close();
+    }
+}

+ 44 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test1.java

@@ -0,0 +1,44 @@
+package com.sf._03_字节输入流;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+public class Test1 {
+    public static void main(String[] args) throws IOException {
+        //1 字节输入流
+        FileInputStream fileInputStream = new FileInputStream(new File("D:\\a.txt"));
+
+        byte[] buffer = new byte[4];
+        int readLen = fileInputStream.read(buffer);
+        System.out.println("读取长度为:"+readLen);   // 4
+        System.out.println("读取内容为:"+ new String(buffer,0,readLen));  //fanj
+        if(readLen !=-1){
+            int readLen1 = fileInputStream.read(buffer);
+            System.out.println("读取长度为:"+readLen1);  // 4
+            System.out.println("读取内容为:"+ new String(buffer,0,readLen1)); //ialo
+
+            if(readLen1!=-1){
+                int readLen2 = fileInputStream.read(buffer);
+                System.out.println("读取长度为:"+readLen2); //4
+                System.out.println("读取内容为:"+ new String(buffer,0,readLen2));  //ngha
+
+                if(readLen2!=-1){
+                    int readLen3 = fileInputStream.read(buffer);
+                    System.out.println("读取长度为:"+readLen3); //2
+                    System.out.println("读取内容为:"+ new String(buffer,0,readLen3));  //ha
+                }else{
+                    System.out.println("已读完");
+                }
+            }else{
+                System.out.println("已经读取完毕");
+            }
+
+        }else{
+            System.out.println("已经读取完毕");
+        }
+
+
+
+    }
+}

+ 31 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test2.java

@@ -0,0 +1,31 @@
+package com.sf._03_字节输入流;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+public class Test2 {
+    public static void main(String[] args) throws IOException {
+        /**
+         * 最终需要通过while 循环进行优化读取内容
+         *
+         */
+        FileInputStream fileInputStream = new FileInputStream(new File("D:\\a.txt"));
+        // 读取长度默认值是-1
+        int len = -1;
+
+        // 创建byte数组, 一次性用于读取多个字节
+        byte[] buffer = new byte[4];
+
+        while ((len = fileInputStream.read(buffer))!=-1){
+            // 打印读取内容
+            System.out.println("读取内容为:"+ new String(buffer,0,len));
+
+        }
+
+        // 把资源关闭掉
+        fileInputStream.close();
+
+
+    }
+}

+ 50 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test3.java

@@ -0,0 +1,50 @@
+package com.sf._03_字节输入流;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+public class Test3 {
+    /**
+     * 已知文档中现内容
+     * name=zhangsan,age=10,email=123
+     *
+     * 把内容封装到Student 对象中打印student
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        //1 创建字节输入流
+        FileInputStream fileInputStream = new FileInputStream(new File("d:\\a.txt"));
+        //2 读取数据
+        byte[] buffer = new byte[100];
+        int len = fileInputStream.read(buffer);
+        String str = new String(buffer, 0, len);
+        System.out.println(str);
+        // 要做事情拿到zhangsan   10  123 把设置student 中
+        //name=zhangsan,age=10,email=123
+        Student student = new Student();
+        String[] strArr = str.split(",");
+        System.out.println(Arrays.toString(strArr));
+        for (String s : strArr) {
+            // s  : name=zhangsan
+            // [name,zhangsan]
+            String[] split = s.split("=");
+            if(s.contains("name")){
+                student.setName(split[1]);
+            }
+            // [age,10]
+            if(s.contains("age")){
+                student.setAge(Integer.valueOf(split[1]));
+            }
+            // [email,132]
+            if(s.contains("email")){
+                student.setEmail(split[1]);
+            }
+        }
+        System.out.println(student);
+        // 关闭
+        fileInputStream.close();
+    }
+}

+ 36 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test4.java

@@ -0,0 +1,36 @@
+package com.sf._03_字节输入流;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class Test4 {
+
+    /**
+     * 已知d盘中有一个视频1.mp4
+     * 要求把1.mp4  复制到d:\\2.mp4
+     *
+     * 操作步骤1
+     * 一次就1024个字节 1MB
+     * 把1 .MP4 读取到程序中 使用输入流
+     * 把内容直接输出到磁盘中 用输出流改名为2.mp4
+     *
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        // 1 创建输入流用于读取文件
+        FileInputStream fileInputStream = new FileInputStream("D:\\1.mp4");
+        // 2 创建输出流用于写文件
+        FileOutputStream fileOutputStream = new FileOutputStream("D:\\2.mp4");
+        // 3 读取数据
+        int len = -1;
+        byte[] buffer = new byte[1024];
+        while ((len = fileInputStream.read(buffer))!=-1){
+            fileOutputStream.write(buffer,0,len);
+        }
+        // 4 关闭资源
+        fileInputStream.close();
+        fileOutputStream.close();
+
+    }
+}

+ 19 - 0
java-base-project10/day22/src/com/sf/_03_字节输入流/Test5.java

@@ -0,0 +1,19 @@
+package com.sf._03_字节输入流;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+public class Test5 {
+
+    public static void main(String[] args) throws IOException {
+        FileInputStream fileInputStream = new FileInputStream(new File("d:\\a.txt"));
+        int len = -1;
+        byte[] buffer = new byte[10];
+        while ((len = fileInputStream.read(buffer))!=-1){
+            System.out.println(len);
+            System.out.println(new String(buffer,0,len));
+        }
+        fileInputStream.close();
+    }
+}

+ 28 - 0
java-base-project10/day22/src/com/sf/_04_字符流/Test.java

@@ -0,0 +1,28 @@
+package com.sf._04_字符流;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class Test {
+    /**
+     * 字符流: 专门用来操作纯文本的
+     * 分成字符输入流 FileReader  字符输出流 FileWriter
+     *
+     * 写方法: write(char)  writer(char[])
+     * 读方法: read()   read(char[])
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        // 使用字符输出流往a.txt 文件中写内容 我是中国人
+        FileWriter fileWriter = new FileWriter(new File("d:\\a.txt"),true);
+        // 写内容
+        fileWriter.write("我是中国人");
+        fileWriter.write(System.lineSeparator());
+        fileWriter.write("我是中国人");
+
+
+        //关闭资源
+        fileWriter.close();
+    }
+}

+ 23 - 0
java-base-project10/day22/src/com/sf/_04_字符流/Test1.java

@@ -0,0 +1,23 @@
+package com.sf._04_字符流;
+
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class Test1 {
+    public static void main(String[] args) throws IOException {
+        /**
+         * 使用字符输入流读取数据
+         */
+        FileReader fileReader = new FileReader(new File("D:\\a.txt"));
+        int len = -1;
+        char[] buffer = new char[10];
+
+        while ((len = fileReader.read(buffer))!=-1){
+            System.out.println(len);
+            System.out.println(new String(buffer,0,len));
+        }
+        fileReader.close();
+    }
+}

+ 35 - 0
java-base-project10/day22/src/com/sf/_05_缓冲流/Test.java

@@ -0,0 +1,35 @@
+package com.sf._05_缓冲流;
+
+import java.io.*;
+
+public class Test {
+    /**
+     * 拷贝d: 1.avi 文件
+     * 分别一次读取1 个字节
+     * 一次读取1024个字节
+     * 使用缓冲流去读取
+     * 看耗时时间
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        FileInputStream fileInputStream = new FileInputStream(new File("D:\\1.mp4"));
+
+        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\3.mp4"));
+        //创建字节缓冲流
+        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
+        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
+        // 一次行读取一个字节
+        long startTime = System.currentTimeMillis();
+        int len = -1;
+        byte[] buffer = new byte[1024];
+        while ((len = bufferedInputStream.read(buffer))!=-1){
+            // 读完直接写出去
+            bufferedOutputStream.write(buffer,0,len);
+        }
+        long endTime = System.currentTimeMillis();
+        System.out.println((endTime-startTime) +"毫秒");
+        // 关闭资源
+        fileInputStream.close();
+        fileOutputStream.close();
+    }
+}

+ 29 - 0
java-base-project10/day22/src/com/sf/_05_缓冲流/Test1.java

@@ -0,0 +1,29 @@
+package com.sf._05_缓冲流;
+
+import java.io.*;
+
+public class Test1 {
+    /**
+     * 把D: a.txt 文件内容按行读取
+     * 写的时候按照行去写
+     *
+     * 要求使用字符缓冲流
+     */
+    public static void main(String[] args) throws IOException {
+        // 创建字符流
+        FileReader fileReader = new FileReader(new File("D:\\a.txt"));
+        FileWriter fileWriter = new FileWriter(new File("D:\\f.txt"));
+        //创建包装流
+        BufferedReader bufferedReader = new BufferedReader(fileReader);
+        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
+
+        String str;
+        while ((str = bufferedReader.readLine())!=null){
+            bufferedWriter.write(str);
+            bufferedWriter.newLine();
+        }
+        // 关闭资源
+        bufferedReader.close();
+        bufferedWriter.close();
+    }
+}

+ 38 - 0
java-base-project10/day22/src/com/sf/_05_缓冲流/Test2.java

@@ -0,0 +1,38 @@
+package com.sf._05_缓冲流;
+
+import java.io.*;
+
+public class Test2 {
+
+    /**
+     * 现在有一个login.log 文件
+     * 里面内容如下 这里面有登录成功日志和登录失败日志
+     * zhangsan - 2026/1/19 10:10 -  success
+     * zhangsan - 2026/1/19 10:10 -  error
+     * lisi - 2026/1/19 10:10   - success;
+     * lisi - 2025/1/19 10:11   - error
+     * wangwu - 2025/1/19 10:12  -error
+     *
+     * 现在要求把登录失败的日志单独输出到error.log
+     * @param args
+     */
+    public static void main(String[] args) throws IOException {
+        FileWriter fileWriter = new FileWriter(new File("D:\\error.log"));
+        FileReader fileReader = new FileReader(new File("D:\\login.log"));
+        // 创建缓冲流
+        BufferedReader bufferedReader = new BufferedReader(fileReader);
+        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
+
+        String str;
+        while ((str = bufferedReader.readLine())!=null){
+            if(str.contains("error")){
+                bufferedWriter.write(str);
+                // 换行
+                bufferedWriter.newLine();
+            }
+        }
+        // 关闭资源
+        bufferedReader.close();
+        bufferedWriter.close();
+    }
+}