【Java代码】:实现文本文件合并

【代码说明】

解释:

基于面向对象的思想,代码的可扩展性强;

利于嫁接其他代码,实现更为强大的功能;

思路:

遍历文件根目录,找出所有要合并的文本文件;

  • 遍历文件夹,然后通过文件过滤器找出文本文件;

以行读的方式读取文本文件,后写入目标文件;

  • 通过基础的IO操作,对每一个符合条件的都进行;

诉求:

给个关注,随时都能更新,希望给出意见和建议;

【实现代码】

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 文本文件的合并
 *
 * @author SUNxRUN
 */
public class TextUnite {

    // 要合并的文本内容最终保存的文件
    public File targetFile = new File("E://AAAAA.txt");

    /**
     * 遍历一个文件目录内的所有相关的文件
     * 利用递归的方法遍历文件夹内所有的文件
     *
     * @param sourceDir 要合并的文本文件所在的根目录
     */
    public void documentTraversal(File sourceDir) {
        if (sourceDir.isFile()) {
            System.out.println(sourceDir.getAbsolutePath());
        } else {
            File[] fileList = sourceDir.listFiles(
                    // 定义要留下的文件
                    (f) -> f.getName().contains(".txt")
            );
            if (fileList != null && fileList.length > 0) {
                for (File f : fileList) {
                    if (f.isFile()) {
                        // 开始合并文本的操作
                        fileCopy(f);
                    } else {
                        documentTraversal(f);
                    }
                }
            }
        }
    }

    /**
     * 复制一个文本文件的内容到另一个文本文件中
     *
     * @param sourceFile 要复制的文本文件
     */
    public void fileCopy(File sourceFile) {

        FileInputStream fis;
        InputStreamReader isr;
        BufferedReader br = null;

        FileOutputStream fos;
        OutputStreamWriter osw;
        BufferedWriter bw;
        PrintWriter pw = null;

        try {
            // 输入流的创建
            fis = new FileInputStream(sourceFile);
            isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
            br = new BufferedReader(isr);
            // 输出流的创建
            fos = new FileOutputStream(targetFile, true);
            osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            bw = new BufferedWriter(osw);
            pw = new PrintWriter(bw, true);

            // 输入一行空行分割文本文件内容
            // 当多个文本文件合并时,便于区分
            pw.println("");
            // 文本内容复制操作
            String line;
            while ((line = br.readLine()) != null) {
                pw.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pw.close();
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        TextUnite textUnite = new TextUnite();
        textUnite.documentTraversal(new File("E:/"));
    }

}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUNxRUN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值