如何检查文件夹是否存在

Al *_*aba 181 java

我正在使用新的Java 7 IO功能,实际上我试图接收文件夹的所有xml文件.但是当文件夹不存在时会引发异常,如何检查该文件夹是否与新IO一起存在?

public UpdateHandler(String release) {
    log.info("searching for configuration files in folder " + release);
    Path releaseFolder = Paths.get(release);
    try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){

        for (Path entry: stream){
            log.info("working on file " + entry.getFileName());
        }
    }
    catch (IOException e){
        log.error("error while retrieving update configuration files " + e.getMessage());
    }


}
Run Code Online (Sandbox Code Playgroud)

Jes*_*per 241

使用java.nio.file.Files:

Path path = ...;

if (Files.exists(path)) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

您可以选择传递此方法LinkOption值:

if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
Run Code Online (Sandbox Code Playgroud)

还有一种方法notExists:

if (Files.notExists(path)) {
Run Code Online (Sandbox Code Playgroud)

  • 另外,请注意`Files.exists(path)`和`Files.notExists(path)`可以同时返回false!这意味着Java无法确定路径是否确实存在. (24认同)
  • Files.isDirectory(Path,LinkOption); (12认同)
  • 文件说明了这一点.:) [link](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html)检查notExists方法无法真正正确链接它. (6认同)
  • @LoMaPh `!Files.exists(path)` 和 `Files.notExists(path)` 不是 100% 一样的东西。当 Java 无法确定文件是否存在时,第一个将返回 `true`,第二个将返回 `false`。 (2认同)

Chr*_*per 193

非常简单:

new File("/Path/To/File/or/Directory").exists();
Run Code Online (Sandbox Code Playgroud)

如果你想确定它是一个目录:

File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

  • 正确答案,但一个小小的通知:`if(f.isDirectory()){...}`就足够了,因为它也检查存在. (40认同)
  • 这不符合OP的问题.`java.io.file`不是OP所指的"新Java 7 IO功能"的一部分.Java 7中引入的`java.nio.file`包提供了`Paths`和`Files`类.这里的其他答案正确地解释了如何使用这些较新的类来解决OP问题. (3认同)

Fer*_*eia 48

要检查新IO是否存在目录:

if (Files.isDirectory(Paths.get("directory"))) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

isDirectorytrue如果文件是目录则返回; false如果文件不存在,不是目录,或者无法确定文件是否是目录.

请参阅:文档.


bvi*_*dal 5

您需要将Path转换为a File并测试存在:

for(Path entry: stream){
  if(entry.toFile().exists()){
    log.info("working on file " + entry.getFileName());
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

不需要单独调用该exists()方法,因为isDirectory()隐式检查目录是否存在。


小智 5

从文件夹目录的字符串生成文件

String path="Folder directory";    
File file = new File(path);
Run Code Online (Sandbox Code Playgroud)

和使用方法存在。
如果要生成文件夹,请使用mkdir()

if (!file.exists()) {
            System.out.print("No Folder");
            file.mkdir();
            System.out.print("Folder created");
        }
Run Code Online (Sandbox Code Playgroud)


小智 5

import java.io.File;
import java.nio.file.Paths;

public class Test
{

  public static void main(String[] args)
  {

    File file = new File("C:\\Temp");
    System.out.println("File Folder Exist" + isFileDirectoryExists(file));
    System.out.println("Directory Exists" + isDirectoryExists("C:\\Temp"));

  }

  public static boolean isFileDirectoryExists(File file)

  {
    if (file.exists())
    {
      return true;
    }
    return false;
  }

  public static boolean isDirectoryExists(String directoryPath)

  {
    if (!Paths.get(directoryPath).toFile().isDirectory())
    {
      return false;
    }
    return true;
  }

}
Run Code Online (Sandbox Code Playgroud)