Issue
I want to create a Quartz job that reads .csv files and moves them when the file is processed. I tried this:
@Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
try {
Files.createDirectory(Path.of(directoryPath.getAbsolutePath() + "/processed"));
} catch (IOException e) {
throw new RuntimeException(e);
}
.......................
}
When I run the code a second time I get the error:
Caused by: java.nio.file.FileAlreadyExistsException: C:\csv\nov\processed
Is there some way to make a check for this directory and skip directory creation? I can remove the line throw new RuntimeException(e);
but I'm looking for a better way to handle the case.
Solution
Did you mean:
try {
Path path = Path.of(directoryPath.getAbsolutePath() + "/processed");
// Check first if the file not exist
if (!Files.exists(path)) {
Files.createDirectory(path);
}
} catch(..)
Answered By - Youcef Laidani
Answer Checked By - Mildred Charles (JavaFixing Admin)