gio*_*ozh 2 filesystems directory android
我有后续的麻烦。我的应用程序应该读取文件列表并向用户显示它们的名称。当应用程序第一次启动时,我需要为应用程序创建一个特定的文件夹吗?那么我如何检查它是否为空?
假设您在内部存储中创建了目录,您可以像这样获取目录中的子对象数
File dir = context.getDir("somePath", Context.MODE_PRIVATE);
File[] children = dir.listFiles();
if(children.length > 0)
{
// the directory is not empty
}
else
{
// the directory is empty.
}
Run Code Online (Sandbox Code Playgroud)
这只是一个快速示例代码。更好的方法是使用自定义FileFilterwith排除 self 和 parent 别名dir.listFiles(),因为我不确定它们是否会始终从结果列表中排除。
| 归档时间: |
|
| 查看次数: |
2541 次 |
| 最近记录: |