5

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?

lospejos
  • 1,976
  • 3
  • 19
  • 35
vitaliy4us
  • 483
  • 5
  • 21
  • 2
    There is a closing bracket at the end without a matching opening bracket, and the indentation is weird. Is there some if statement missing from the code? – Florian Albrecht Jan 16 '17 at 13:21
  • Could you provide a sample of the output? – phss Jan 16 '17 at 13:23
  • 4
    Why not try calling `files[i].delete()`, and see what exception is thrown. – Andy Turner Jan 16 '17 at 13:28
  • 1
    Try using the jdk File.delete method instead of deleteQuietly. I'm hoping it would throw some sort of exception. It would be much easier to debug if there is an error to solve. – Curious Jan 16 '17 at 13:29
  • 1
    Seconded, `FileUtils.deleteQuietly()` "Deletes a file, never throwing an exception." quoting their javadoc, so debugging with this method won't be possible. – Aaron Jan 16 '17 at 13:33
  • There are two different issues: why the files aren't being deleted and why there is no error. Concerning the former, it would be interesting to see a long directory ("ls -l") listing to show the file permissions before the code is run, and to know what user is running the code. – Thomas Hedden Jan 16 '17 at 13:33
  • This is my output: ********** files found: 5 .\img\1.abc delete result = true .\img\1.acd delete result = true .\img\1.adc delete result = true .\img\1.jpg delete result = false .\img\1.png delete result = false – vitaliy4us Jan 16 '17 at 13:40
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow, because your code appears to work as designed. –  May 28 '18 at 07:18

2 Answers2

1

Try this:

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

And see what exception will be thrown.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
hoat4
  • 1,182
  • 12
  • 9
0

This code is working for me:

File dir = new File("/home/abhijith/.img");
        FileFilter fileFilter = new WildcardFileFilter("abc"+".*");
        File[] files = dir.listFiles(fileFilter);
        System.out.println("files found: " + files.length);
        for (int i = 0; i < files.length; i++) {
            boolean success = org.apache.commons.io.FileUtils.deleteQuietly(files[i]);
            System.out.println(files[i] + " delete result = " + success);
        }

Here is the output:

files found: 1
/home/abhijith/.img/abc.jpg delete result = true
Abhijith S
  • 526
  • 5
  • 17