How to delete a file?

I have a problem when deleting files from the 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);
    }
}

In fact, the code is used to replace the image files existing in the img directory with a new one. I need to delete all pre-existing files whose names are n. *, With a new file, for example. n.png. If I try to delete image files, I get a false value for the success of the variable, and the files are not deleted. But not image files, for example. * .abc; * .acd; * .acdc etc. deleted successfully. What is the problem?

+4
source share
2 answers

Try the following:

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

And look what exception will be thrown.

+1
source

, , , , . , .

0

Source: https://habr.com/ru/post/1666912/


All Articles