Check Directory Empty Java

Check Directory Empty Java

If you want to search directory and check if it is empty or not using java please have sample program below. We will use jdk 7 Files.newDirectoryStream open directory and use iterator to check if it is empty or not.

  • Sample java program:

  • Output:


For more information about Files API please check oracle documentation here

2 thoughts on “Check Directory Empty Java

  1. You need to close that DirectoryStream when you’re done with it, which you can do easily by wrapping it in a try-with-resources.

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *