簡體   English   中英

如果兩個文件相同並且在Java中具有相同的內容,則提供日志

[英]Provide log if the two files are identical and has same contents in Java

我有下面的代碼,我從特定目錄讀取文件,處理它,一旦處理,我將文件移動到存檔目錄。 這工作正常。 我每天都會收到新文件,並且正在使用 Control-M 調度程序作業來運行此進程。

現在在下一次運行中,我再次從該特定目錄中讀取新文件,並使用存檔目錄中的文件檢查該文件,如果內容不同,則僅處理該文件,否則不執行任何操作。 有編寫 shell 腳本來完成這項工作,我們沒有看到此過程的任何日志。

現在,如果文件與特定目錄和存檔目錄中的文件相同,我想在我的 Java 代碼中生成日志消息,然后生成“文件相同”的日志。 但我不知道如何做到這一點。 我不想編寫處理或移動文件中任何內容的邏輯..我只需要檢查文件是否相等,然后生成日志消息。 我收到的文件不是很大,最大大小可以達到 10MB。

下面是我的代碼:

        for(Path inputFile : pathsToProcess) {
            // read in the file:
            readFile(inputFile.toAbsolutePath().toString());
            // move the file away into the archive:
            Path archiveDir = Paths.get(applicationContext.getEnvironment().getProperty(".archive.dir"));
            Files.move(inputFile, archiveDir.resolve(inputFile.getFileName()),StandardCopyOption.REPLACE_EXISTING);
        }
        return true;
    }

    private void readFile(String inputFile) throws IOException, FileNotFoundException {
        log.info("Import " + inputFile);

        try (InputStream is = new FileInputStream(inputFile);
                Reader underlyingReader = inputFile.endsWith("gz")
                        ? new InputStreamReader(new GZIPInputStream(is), DEFAULT_CHARSET)
                        : new InputStreamReader(is, DEFAULT_CHARSET);
                BufferedReader reader = new BufferedReader(underlyingReader)) {

            if (isPxFile(inputFile)) {
                Importer.processField(reader, tablenameFromFilename(inputFile));
            } else {
                Importer.processFile(reader, tablenameFromFilename(inputFile)); 
            }

        }
        log.info("Import Complete");
    }       

}

基於關於文件大小或性能需求的有限信息,可以做這樣的事情。 這可能不是 100% 優化的,而只是一個例子。 您可能還需要在 main 方法中進行一些異常處理,因為新方法可能會拋出 IOException:

import org.apache.commons.io.FileUtils;  // Add this import statement at the top


// Moved this statement outside the for loop, as it seems there is no need to fetch the archive directory path multiple times.
Path archiveDir = Paths.get(applicationContext.getEnvironment().getProperty("betl..archive.dir"));  

for(Path inputFile : pathsToProcess) {

    // Added this code
    if(checkIfFileMatches(inputFile, archiveDir); {
        // Add the logger here.
    }
    //Added the else condition, so that if the files do not match, only then you read, process in DB and move the file over to the archive. 
    else {
        // read in the file:
        readFile(inputFile.toAbsolutePath().toString());
        Files.move(inputFile, archiveDir.resolve(inputFile.getFileName()),StandardCopyOption.REPLACE_EXISTING);
    }       
}


//Added this method to check if the source file and the target file contents are same.
// This will need an import of the FileUtils class. You may change the approach to use any other utility file, or read the data byte by byte and compare. If the files are very large, probably better to use Buffered file reader.
    private boolean checkIfFileMatches(Path sourceFilePath, Path targetDirectoryPath) throws IOException {
        if (sourceFilePath != null) {  // may not need this check
            File sourceFile = sourceFilePath.toFile();
            String fileName = sourceFile.getName();

            File targetFile = new File(targetDirectoryPath + "/" + fileName);

            if (targetFile.exists()) {
                return FileUtils.contentEquals(sourceFile, targetFile);
            }
        }
        return false;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM