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

Question:

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

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

   if(fileName.equals(f.getName())){
         какой то код
    }
  }

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?

Answer:

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