如何检查文件夹是否为空

Beg*_*ner 6 android

我目前需要从文件夹发送文件,我希望我运行的服务每隔半小时检查一次文件夹....我怎么能知道文件夹是否清晰?

I82*_*uch 26

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 {

}
Run Code Online (Sandbox Code Playgroud)

  • 很高兴能够检查空虚或文件数量,而无需将它们全部加载到内存中.可惜. (3认同)

Wil*_*ate 5

if (file.isDirectory()) {
    String[] files = file.list();
    if (files.length == 0) {
        //directory is empty
    }
}
Run Code Online (Sandbox Code Playgroud)