Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file properly? [duplicate]

I have a problem while deleting files from img directory using org.apache.commons.io.FileUtils. I am using this code:

File dir = new File(".\\img");
    FileFilter fileFilter = new WildcardFileFilter(userId + ".*");
    File[] files = dir.listFiles(fileFilter);
    System.out.println("files found: " + files.length);
    for (int i = 0; i < files.length; i++) {
        boolean success = FileUtils.deleteQuietly(files[i]);
        System.out.println(files[i] + " delete result = " + success);
    }
}

Actually the code is using for replacing image files existed in img directory with the new one. I need to delete all previously existed files which names are n.*, with the new file e.g. n.png. If I am trying to delete image files, I get false value for the variable success and the files are not deleted. But not image files e.g. *.abc; *.acd; *.acdc etc. are deleted succesfully. What is the case of this issue?

like image 306
vitaliy4us Avatar asked Nov 09 '22 03:11

vitaliy4us


1 Answers

Try this:

java.nio.file.Files.delete(files[i].toPath());

And see what exception will be thrown.

like image 56
hoat4 Avatar answered Nov 15 '22 06:11

hoat4