0

如果它们存在,我需要检查至少 4 个目录,并在变量中获取正确的路径以完成我的代码。

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

谢谢你的帮助。

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

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");
    }
4

2 回答 2

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

}
于 2013-09-09T03:18:23.383 回答
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);
}
于 2013-09-09T02:18:50.003 回答