I'am trying to copy a file to a directory and then deleting it, but file.delete() keeps returning false
Here is my code:
for (File file : list) {
if (!file.isDirectory()) {
try {
FileUtils.copyFileToDirectory(file, path);
file.setWritable(true);
System.out.println(file.delete());
if(file.exists()){
file.deleteOnExit();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
Few ideas that you can work it out.
Use Files.delete. The delete(Path) method deletes the file or throws an exception if the deletion fails. For example, if the file does not exist a NoSuchFileException is thrown. You can catch the exception to determine why the delete failed as follows: See Oracle docs here