0

I need one help. In our application we are deleting some directory and files from disk space through a job Sometime this job takes much time due to large number of directories and meanwhile that job gets triggerred for second time and its trying to delete some files which are already deleted by the first job. As a result we are getting Null Pointer Exception in for (File file : files) line because parent.listFiles() is returning null

To fix this is issue, I can add a null check. But I was thinking if I can use Stream API because I think from performance perspective its little bit ahead of other approach( Please correct me if I am wrong).

So my question is :

  1. Is there any null check in the foreach() method(mentioned below) ?In other words is it safe to use ? I checked that method but I didnt understand much

  2. Please suggest if there is any better approach than this.

Code which is having issue :

  public void deleteFile(File parent) {

        if (parent.isDirectory()) {
            File[] files = parent.listFiles();
            for (File file : files) {
                deleteFile(file);
            }

        }
        parent.delete();
    }

Solution :

Files.walk(rootPath)
            .sorted(Comparator.reverseOrder())
            .map(Path::toFile)
            .peek(System.out::println)
            .forEach(java.io.File::delete);
Rahman
  • 3,755
  • 3
  • 26
  • 43
  • 1
    Files.deleteIfExists(Path)? Also, see this: [Java.nio: most concise recursive directory delete](https://stackoverflow.com/questions/35988192/java-nio-most-concise-recursive-directory-delete). – prasad_ Aug 09 '18 at 05:43
  • 2
    Possible duplicate of [Enhanced for loop and null check of objects](https://stackoverflow.com/questions/26759954/enhanced-for-loop-and-null-check-of-objects) – karamazovbros Aug 09 '18 at 05:49
  • 1
    There is no additional null check using Streams. Only `Optional.isPresent()` has a null check. – Peter Lawrey Aug 09 '18 at 06:00
  • as a side note, how about putting those tasks into a queue that will be handled by a single Thread and thus letting each task finish before starting a new one? – Eugene Aug 09 '18 at 08:48

3 Answers3

1

Consider having null check like:

Files.walk(rootPath)
        .filter(Objects:notNull)
        .sorted(Comparator.reverseOrder())
        .map(Path::toFile)
        .peek(System.out::println)
        .forEach(java.io.File::delete);

But not sure how would Files.walk yield a null object.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
0

forEach is a method which runs if there is data i.e in your case files , if there is no file then control will come out of forEach.

We do not need to check for null inside forEach.

Shriharsha
  • 27
  • 2
0

There is not null check. foreach construct in java is great candidate for NullPointerException

File[] files = null;

 for (File file : files) {
    deleteFile(file);
 }

You should allways check !!!

 if(files !=null) {
     for (File file : files) {
       deleteFile(file);
    }
 }

see How does the Java 'for each' loop work?

user1722245
  • 2,065
  • 1
  • 18
  • 31