How to check if a folder is empty

I currently need to send files from a folder, I want the service that I run to check in the folder every half an hour .... how can I find out if I have a free file?

+4
source share
3 answers
File directory = new File("/path/to/folder"); File[] contents = directory.listFiles(); // the directory file is not really a directory.. if (contents == null) { } // Folder is empty else if (contents.length == 0) { } // Folder contains files else { } 
+23
source
 if (file.isDirectory()) { String[] files = file.list(); if (files.length == 0) { //directory is empty } } 
+3
source

If you have a path, you can check the File object for entries (using file.isDirectory () and file.list ())

+2
source

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


All Articles