Answers:
假设path是你的String。
File file = new File(path);
boolean exists =      file.exists();      // Check if the file exists
boolean isDirectory = file.isDirectory(); // Check if it's a directory
boolean isFile =      file.isFile();      // Check if it's a regular file
或者,您可以使用NIO类Files并检查如下内容:
Path file = new File(path).toPath();
boolean exists =      Files.exists(file);        // Check if the file exists
boolean isDirectory = Files.isDirectory(file);   // Check if it's a directory
boolean isFile =      Files.isRegularFile(file); // Check if it's a regular file
              path在我的例子中是String。为什么不能创建File实例?请注意,这不会更改文件系统上的任何内容。
                    root可能是一个文件。文件不一定具有.something扩展名。
                    保持nio API的同时,清洁解决方案:
Files.isDirectory(path)
Files.isRegularFile(path)
              File每次都创建一个新对象。节省内存
                    请坚持使用nio API进行这些检查
import java.nio.file.*;
static Boolean isDir(Path path) {
  if (path == null || !Files.exists(path)) return false;
  else return Files.isDirectory(path);
}
              String path = "Your_Path";
File f = new File(path);
if (f.isDirectory()){
  }else if(f.isFile()){
  }
              如果文件系统中不存在String a file或directory,系统将无法告诉您。例如:
Path path = Paths.get("/some/path/to/dir");
System.out.println(Files.isDirectory(path)); // return false
System.out.println(Files.isRegularFile(path)); // return false
对于以下示例:
Path path = Paths.get("/some/path/to/dir/file.txt");
System.out.println(Files.isDirectory(path));  //return false
System.out.println(Files.isRegularFile(path));  // return false
因此,我们看到两种情况下系统都返回false。这是真实的两个java.io.File和java.nio.file.Path
   private static boolean isValidFolderPath(String path) {
    File file = new File(path);
    if (!file.exists()) {
      return file.mkdirs();
    }
    return true;
  }