简体   繁体   English

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

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

I have below code where i am reading the file from particular directory, processing it and once processed i am moving the file to archive directory.我有下面的代码,我从特定目录读取文件,处理它,一旦处理,我将文件移动到存档目录。 This is working fine.这工作正常。 I am receiving new file everyday and i am using Control-M scheduler job to run this process.我每天都会收到新文件,并且正在使用 Control-M 调度程序作业来运行此进程。

Now in next run i am reading the new file from that particularly directory again and checking this file with the file in the archive directory and if the content is different then only process the file else dont do anything.现在在下一次运行中,我再次从该特定目录中读取新文件,并使用存档目录中的文件检查该文件,如果内容不同,则仅处理该文件,否则不执行任何操作。 There is shell script written to do this job and we dont see any log for this process.有编写 shell 脚本来完成这项工作,我们没有看到此过程的任何日志。

Now i want to produce log message in my java code if the files are identical from the particular directory and in the archive directory then generate log that 'files are identical'.现在,如果文件与特定目录和存档目录中的文件相同,我想在我的 Java 代码中生成日志消息,然后生成“文件相同”的日志。 But i dont know exactly how to do this.但我不知道如何做到这一点。 I dont want to write the the logic to process or move anything in the file ..i just need to check the files are equal and if it is then produce log message.我不想编写处理或移动文件中任何内容的逻辑..我只需要检查文件是否相等,然后生成日志消息。 The file which i recieve are not very big and the max size can be till 10MB.我收到的文件不是很大,最大大小可以达到 10MB。

Below is my code:下面是我的代码:

        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");
    }       

}

Based on the limited information about the size of file or performance needs, something like this can be done.基于关于文件大小或性能需求的有限信息,可以做这样的事情。 This may not be 100% optimized, but just an example.这可能不是 100% 优化的,而只是一个例子。 You may also have to do some exception handling in the main method, since the new method might throw an IOException:您可能还需要在 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