ChinaTextbook icon indicating copy to clipboard operation
ChinaTextbook copied to clipboard

合并文件查找太麻烦了.用java写了个工具类,需要的自己取.windwos下Java环境

Open WuxuFanhua opened this issue 11 months ago • 2 comments

自己修改path路径 还有下载的合并工具路径

import cn.hutool.core.io.FileUtil; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader;

public class MergePDF { private static File exeSource = new File("C:\Users\XXXX\Downloads\mergePDFs.exe"); public static void main(String[] args) { String path = "E:\download\ChinaTextbook-master"; File rootDir = new File(path); if (!rootDir.exists() || !rootDir.isDirectory()) { System.out.println("无效的根目录路径"); return; } // 递归查找并处理 processDirectories(rootDir); }

/**
 * 递归处理目录及其子目录
 */
private static void processDirectories(File dir) {
    File[] files = dir.listFiles();
    if (files == null) return;

    for (File file : files) {
        if (file.isDirectory()) {
            // 如果是目录,先递归处理其子目录
            processDirectories(file);
        } else {
            // 检查当前目录名是否以 .pdf.1 结尾
            if (file.getName().endsWith(".pdf.1")) {
                handlePdf1Folder(file);
                break;
            }
        }
    }
}

/**
 * 处理匹配到的 .pdf.1 文件夹
 */
private static void handlePdf1Folder(File folder) {

    
    File destDir = folder.getParentFile();
    File destExe = new File(destDir, "mergePDFs.exe");

    // 如果目标目录下没有 mergePDFs.exe,则复制过去
    if (!destExe.exists()) {
        try {
            FileUtil.copy(exeSource, destExe, true);
        } catch (Exception e) {
            System.err.println("复制失败: " + folder);
            e.printStackTrace();
        }
    }

    // 执行合并命令
    if (destExe.exists()) {
        try {
            System.out.println(destExe.getAbsolutePath());
            Thread.sleep(100);
            try {
                ProcessBuilder builder = new ProcessBuilder(destExe.getAbsolutePath());
                builder.directory(new File(destDir.getAbsolutePath()));
                Process process = builder.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

WuxuFanhua avatar May 16 '25 09:05 WuxuFanhua

https://github.com/TapXWorld/ChinaTextbook/issues/41

有python版的。

TapXWorld avatar May 16 '25 11:05 TapXWorld

Mac 怎么合并啊,求

wyqing9 avatar Jun 23 '25 12:06 wyqing9