java – How to set a condition if the folder does not contain files?


Good afternoon, I get a list of files from a folder

File folder = new File("D://Photo_and_Video//");// доступ к папке с файлами
            File[] listOfFiles = folder.listFiles();// получаем список файлов
            for (File f : listOfFiles) {

         какой то код

and, accordingly, I check for the coincidence of names, but then the question arose: how to set a condition, to do something if the folder does not contain files, that is, folder.listFiles () does not return anything?


if(listOfFiles.length > 0) {
    for(File f : listOfFiles) {
else {
    System.out.println("Пустая папка");
Scroll to Top