2017-12-13 2 views
0

誰かがJava内からこの操作を手助けできるかどうかは疑問です。Javaでログファイル(gzipped)を組み合わせる

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

a_log_file.log.gz.1a_log_file.log.gz.2 ... 私はむしろ複雑ではない何かを見つけることができませんでした。私は何とかJavaからこれを実行することもできますが、それは間違った解決策のように感じます。

+0

[this](https://codereview.stackexchange.com/questions/77583/copying-contents-of-two-files-into-one-file-bufferedreader-close-コール)? –

答えて

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 

例外処理を行う必要があります。また、これはすでにgzipされたログを行いません。私はその部分を仮定しました