我有后续的麻烦。我的应用程序应读取文件列表并向用户显示其名称。 当应用程序第一次启动时,我需要为应用程序创建一个特定的文件夹? 那么我该如何检查它是否为空?检查一个文件夹是否为空(内部存储)
1
A
回答
4
假设您创建在内部存储的目录,你可以得到子对象的数量在目录这样
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.
}
这仅仅是一个快速的示例代码。更好的方法是使用自定义FileFilter
和dir.listFiles()
排除自我和父代别名,因为我不确定它们将始终从结果列表中排除。
2
下面的代码将检查是否有文件夹已经存在,如果不创建一个,并警告你,如果错误创建一个:如果路径存在与否和path.exists()
简单地为您创建一个
// check if appfolder is created and if not through an
// exception
File path = new File("yourDir");
if (!path.exists()) {
if (!path.mkdirs()) {
try {
throw new IOException(
"Application folder can not be created. Please check if your memory is writable and healthy and then try again.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
} else {
Log.i("app", "directory is: " + path.getPath());
}
exists()
检查。
相关问题
- 1. 试图检查内部存储器中是否存在文件
- 2. AJAX检查一个外部文件的div是否为空
- 3. PHP - 检查一个文件夹中是否存在文件
- 4. 检查IMAP是否存在文件夹
- 5. AngularJS - 检查文件夹是否存在
- 6. 内部存储文件夹和文件
- 7. 试图保存并检查文件是否保存在内部存储
- 8. 如何检查一个文件夹是否存在使用PHP
- 9. 检查一个文件夹是否存在
- 10. 检查文件夹是否为空(什么是“。”和“..”)
- 11. 外部应用程序:检查是否Outlook文件夹存在
- 12. 如何检查文件夹是否为空
- 13. 如何检查循环中的文件夹是否为空?
- 14. 无法检查文件夹是否为空
- 15. 内部存储不是存储文件
- 16. Java检查文件是否为空:IOException:null
- 17. 检查文件是否为空?
- 18. 检查Excel文件是否为空C#
- 19. ANT:检查文件是否为空
- 20. C++检查文件是否为空
- 21. 检查文件是否为空
- 22. 检查xml文件是否为空
- 23. 检查文件是否为空或不
- 24. Python - 检查文件是否为空
- 25. VB检查一个文件夹里面是否有文件
- 26. 检查文件是否属于一个文件夹
- 27. 检查一个变量是否为空
- 28. 检查一个值是否为空
- 29. 检查一个NSMutableDictionary是否为空?
- 30. 检查一个表是否为空