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();
    }
}
