检查一些文件夹是否存在java Android

时间:2013-09-09 02:01:21

标签: java android

我需要检查至少4个目录(如果它们存在)并在变量中为finnish我的代码获取正确的路径。

但我不知道这样做的正确方法。

感谢您的帮助。

这里是我检查单个目录的代码

final String uploadFilePath = "/mnt/sdcard/folder1/";

   File f = new File(uploadFilePath);

    if(f.exists() && f.isDirectory()){
        Log.v("FILES", "EXIST");
    }else{
        Log.v("FILES", "DONT EXIST");
    }

2 个答案:

答案 0 :(得分:3)

这样你可以继续

String[] myDirectories = {"","",""......}; // your list of directories

for (String directory : myDirectories ) {
  File file = new File(directory);
   if(file.exists() && file.isDirectory())
      // Do something you have found your directory

}

答案 1 :(得分:0)

这够了吗?

Path uploadPath = Paths.get(uploadFilePath);
Path path = Files.exists(uploadPath) ?  uploadPath : Files.createDirectory(uploadPath);
System.out.println(path);

然后检查N个目录,将它放在循环中。

final String[] paths = { "C:/aa/", "C:/bb/", "C:/cc/", "C:/dd/"};
for (String path : paths) {
   Path uploadPath = Paths.get(path);
   if(Files.exists(uploadPath))
       Files.createDirectory(uploadPath);
   System.out.println(uploadPath);
}