0

我想知道是否有人可以帮助我从 Java 中完成此操作。

zcat -f -- $(ls -t a_log_file.log*) > combined.log

哪里有a_log_file.log, a_log_file.log.gz.1, a_log_file.log.gz.2... 我找不到任何不是很复杂的东西。我想,我也可以以某种方式从 Java 运行它,但这感觉像是一个错误的解决方案。

4

1 回答 1

0
File logDir = new File("path/to/log/files");
    File[] nonGzippedFiles = logDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return !pathname.getName().contains("gz");
        }
    });
    FileOutputStream combinedLogFiles = new FileOutputStream(new File("path/to/combined.log"));
    for (File nonGzippedFile : nonGzippedFiles) {
        FileInputStream fileInputStream = new FileInputStream(nonGzippedFile);
        int read = 0;
        byte[] buff = new byte[128];
        while ((read = fileInputStream.read(buff)) > 0) {
            combinedLogFiles.write(buff, 0, read);
        }
        fileInputStream.close();
    }
    combinedLogFiles.close();

    // path/to/combined.log now contains all the contents of the log files

您必须进行一些异常处理。此外,这不会执行已压缩的日志。我假设那部分

于 2017-12-13T03:07:42.803 回答