I want to delete one file and rename another file with the old file but I am not able to move this file as java is throwing java.nio.file.FileAlreadyExistsException
Following is the code snippet I am using
static void swapData(String origFilePath, String tempFilePath) throws IOException{
Path tempPath = FileSystems.getDefault().getPath(tempFilePath);
Path origPath = FileSystems.getDefault().getPath(origFilePath);
try{
String origFileName = null;
File origFileRef = new File(origFilePath);
if(Files.exists(origPath)){
origFileName = origFileRef.getName();
Files.delete(origPath);
if(Files.exists(origPath))
throw new IOException("cannot able to delete original file");
}
if(origFileName != null)
Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
}catch(IOException e){
throw e;
}
}
Here is the exception I am recieving
on Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
Also when I see this file in windows explorer, its thumbnail is present but cannot able to open it. I am not able to understand why it is happening and If I am using REPLACE_EXISTING, why it is throwing FileAlreadyExistsException exception.
Also I edited the previous question as it is not clearly stated.
Please help.
Anuj