java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: 进程无法访问该文件,因为它正被 java 中的另一个进程使用

java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: The process cannot access the file because it is being used by another process in java

我有spring批次,这里我从文件夹中检索文件,然后使用OpenCV库读取文件,代码是这样的。

 List<SendingFile> sendCloudFile = new CsvToBeanBuilder<SendingFile>(new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_8))
                        .withType(SendingFile.class)
                        .build()
                        .parse();

之后,当我尝试删除同一个文件时,出现错误。 (该进程无法访问该文件,因为它正被另一个进程使用。) 我在这里缺少什么?

@Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        if (stepExecution.getExitStatus().equals(ExitStatus.FAILED)) {
            service.updateErrorStatus(mailApplicationId, null, null);
        }
        for (Map.Entry<String, String> processedFile : processedFileList.entrySet()) {
            File newFile = new File(recoveryFolder, processedFile.getKey());
            try {
                Files.move(Paths.get(processedFile.getValue()), Paths.get(recoveryFolder, processedFile.getKey()), REPLACE_EXISTING);
            } catch (Exception ex) {
                log.warn("Error in moving the file. ");
                log.warn(ex.getMessage());
            }
        }
        return stepExecution.getExitStatus();
    }

这是一个已知问题:https://github.com/spring-projects/spring-batch/issues/1500

解决方法是使用不同的步骤来移动文件,而不是在侦听器中执行。