读取文件内容,根据关键字删除某几行数据

读取文件内容,根据关键字删除某几行数据

效果展示

source.txt 源文件
target.txt 目标文件
在这里插入图片描述

eg: keyword import
target:import包含的那几行已经删除,重新生成到target.txt文件里面

source.txt

package com.geekmice.springbootselfexercise.utils;

import com.geekmice.springbootselfexercise.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Objects;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.utils
 * @Author: pingmingbo
 * @CreateTime: 2023-08-15  21:36
 * @Description: 文件操作工具类
 * @Version: 1.0
 */
@Slf4j
public class FileUtil {

    /**
     * 文件目录
     */
    public static final String PATH_PREFIX = "D:\\huasukeji\\spring-boot-self-exercise\\src\\main\\java\\com\\geekmice\\springbootselfexercise\\file";

    /**
     * 根据关键字删除文件内容所在行,生成新的文件
     * @param absolutePath 文件地址
     * @param keyword 关键字
     */
    public static void delFileContentByKeyword(String absolutePath, String keyword) {
        StringBuilder concatBuffer = new StringBuilder();
        concatBuffer.append(PATH_PREFIX);
        if (StringUtils.isEmpty(absolutePath)) {
            absolutePath = "source.txt";
        }
        concatBuffer.append(absolutePath);
        StringBuilder buffer = new StringBuilder();
        int lineNum = 1;
        /**
         * 1 读取文件内容
         */
        try (BufferedReader reader = new BufferedReader(new FileReader(concatBuffer.toString()))) {
            while (true) {
                String line = reader.readLine();
                if (Objects.isNull(line)) {
                    log.info("第【{}】是最后一行", lineNum);
                    break;
                }

                if (line.contains(keyword)) {
                    log.info("删除的行号:【{}】", lineNum);
                    lineNum++;
                    continue;
                }
                buffer.append(line).append("\n");
                log.info("第【{}】,line:【{}】", lineNum, line);
                lineNum++;
            }
        } catch (IOException e) {
            log.error("params:【{}】,error msg:【{}】", lineNum, lineNum);
            lineNum++;
        }

        /**
         * 2 写入文件内容
         */
        String targetPath = concatBuffer.toString();
        String handlePath = targetPath.replace("source", "target");
        try (PrintWriter printWriter = new PrintWriter(handlePath)) {
            log.info("targetPath:【{}】", handlePath);
            printWriter.println(buffer.toString());
        } catch (FileNotFoundException e) {
            log.error("params :【{}】,error msg:【{}】", handlePath, e);
            throw new BusinessException("error msg:【".concat(e.toString()).concat("】"));
        }

    }
}

target.txt

package com.geekmice.springbootselfexercise.utils;



/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.utils
 * @Author: pingmingbo
 * @CreateTime: 2023-08-15  21:36
 * @Description: 文件操作工具类
 * @Version: 1.0
 */
@Slf4j
public class FileUtil {

    /**
     * 文件目录
     */
    public static final String PATH_PREFIX = "D:\\huasukeji\\spring-boot-self-exercise\\src\\main\\java\\com\\geekmice\\springbootselfexercise\\file";

    /**
     * 根据关键字删除文件内容所在行,生成新的文件
     * @param absolutePath 文件地址
     * @param keyword 关键字
     */
    public static void delFileContentByKeyword(String absolutePath, String keyword) {
        StringBuilder concatBuffer = new StringBuilder();
        concatBuffer.append(PATH_PREFIX);
        if (StringUtils.isEmpty(absolutePath)) {
            absolutePath = "source.txt";
        }
        concatBuffer.append(absolutePath);
        StringBuilder buffer = new StringBuilder();
        int lineNum = 1;
        /**
         * 1 读取文件内容
         */
        try (BufferedReader reader = new BufferedReader(new FileReader(concatBuffer.toString()))) {
            while (true) {
                String line = reader.readLine();
                if (Objects.isNull(line)) {
                    log.info("第【{}】是最后一行", lineNum);
                    break;
                }

                if (line.contains(keyword)) {
                    log.info("删除的行号:【{}】", lineNum);
                    lineNum++;
                    continue;
                }
                buffer.append(line).append("\n");
                log.info("第【{}】,line:【{}】", lineNum, line);
                lineNum++;
            }
        } catch (IOException e) {
            log.error("params:【{}】,error msg:【{}】", lineNum, lineNum);
            lineNum++;
        }

        /**
         * 2 写入文件内容
         */
        String targetPath = concatBuffer.toString();
        String handlePath = targetPath.replace("source", "target");
        try (PrintWriter printWriter = new PrintWriter(handlePath)) {
            log.info("targetPath:【{}】", handlePath);
            printWriter.println(buffer.toString());
        } catch (FileNotFoundException e) {
            log.error("params :【{}】,error msg:【{}】", handlePath, e);
            throw new BusinessException("error msg:【".concat(e.toString()).concat("】"));
        }

    }
}


代码实现

package com.geekmice.springbootselfexercise.utils;

import com.geekmice.springbootselfexercise.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Objects;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.utils
 * @Author: pingmingbo
 * @CreateTime: 2023-08-15  21:36
 * @Description: 文件操作工具类
 * @Version: 1.0
 */
@Slf4j
public class FileUtil {

    /**
     * 文件目录
     */
    public static final String PATH_PREFIX = "D:\\huasukeji\\spring-boot-self-exercise\\src\\main\\java\\com\\geekmice\\springbootselfexercise\\file\\";

    /**
     * 根据关键字删除文件内容所在行,生成新的文件
     * @param absolutePath 文件地址
     * @param keyword 关键字
     */
    public static void delFileContentByKeyword(String absolutePath, String keyword) {
        StringBuilder concatBuffer = new StringBuilder();
        concatBuffer.append(PATH_PREFIX);
        if (StringUtils.isEmpty(absolutePath)) {
            absolutePath = "source.txt";
        }
        concatBuffer.append(absolutePath);
        StringBuilder buffer = new StringBuilder();
        int lineNum = 1;
        /**
         * 1 读取文件内容
         */
        try (BufferedReader reader = new BufferedReader(new FileReader(concatBuffer.toString()))) {
            while (true) {
                String line = reader.readLine();
                if (Objects.isNull(line)) {
                    log.info("第【{}】是最后一行", lineNum);
                    break;
                }

                if (line.contains(keyword)) {
                    log.info("删除的行号:【{}】", lineNum);
                    lineNum++;
                    continue;
                }
                buffer.append(line).append("\n");
                log.info("第【{}】,line:【{}】", lineNum, line);
                lineNum++;
            }
        } catch (IOException e) {
            log.error("params:【{}】,error msg:【{}】", lineNum, lineNum);
            lineNum++;
        }

        /**
         * 2 写入文件内容
         */
        String targetPath = concatBuffer.toString();
        String handlePath = targetPath.replace("source", "target");
        try (PrintWriter printWriter = new PrintWriter(handlePath)) {
            log.info("targetPath:【{}】", handlePath);
            printWriter.println(buffer.toString());
        } catch (FileNotFoundException e) {
            log.error("params :【{}】,error msg:【{}】", handlePath, e);
            throw new BusinessException("error msg:【".concat(e.toString()).concat("】"));
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值