如果两个文件相同并且在java中具有相同的内容,则提供日志记录

wko9yo5t  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(355)

我有下面的代码,我从特定的目录中读取文件,处理它,一旦处理,我将文件移动到存档目录。这很有效。我每天都会收到新文件,我使用control-m调度程序作业来运行这个过程。
现在在下一次运行中,我将再次从该目录中读取新文件,并将该文件与存档目录中的文件进行检查,如果内容不同,则只处理该文件,否则不执行任何操作。有一个shell脚本写来做这项工作,我们没有看到这个过程的任何日志。
现在我想在java代码中生成日志消息,如果特定目录中的文件和归档目录中的文件相同,那么生成“files are idential”的日志。但我不知道该怎么做。我不想写逻辑来处理或移动文件中的任何东西。我只需要检查文件是否相等,如果相等,然后生成日志消息。我收到的文件不是很大,最大可以到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");
    }       

}
zour9fqk

zour9fqk1#

基于有关文件大小或性能需求的有限信息,可以这样做。这可能不是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;
    }

相关问题