How do I check if a file exists in Java?

后端 未结 17 2412
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:50

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl\'s -e $filename

相关标签:
17条回答
  • 2020-11-22 01:19

    Don't use File constructor with String.
    This may not work!
    Instead of this use URI:

    File f = new File(new URI("file:///"+filePathString.replace('\\', '/')));
    if(f.exists() && !f.isDirectory()) { 
        // to do
    }
    
    0 讨论(0)
  • 2020-11-22 01:22

    For me a combination of the accepted answer by Sean A.O. Harney and the resulting comment by Cort3z seems to be the best solution.

    Used the following snippet:

    File f = new File(filePathString);
    if(f.exists() && f.isFile()) {
        //do something ...
    }
    

    Hope this could help someone.

    0 讨论(0)
  • 2020-11-22 01:24

    By using nio in Java SE 7,

    import java.nio.file.*;
    
    Path path = Paths.get(filePathString);
    
    if (Files.exists(path)) {
      // file exist
    }
    
    if (Files.notExists(path)) {
      // file is not exist
    }
    

    If both exists and notExists return false, the existence of the file cannot be verified. (maybe no access right to this path)

    You can check if path is a directory or regular file.

    if (Files.isDirectory(path)) {
      // path is directory
    }
    
    if (Files.isRegularFile(path)) {
      // path is regular file
    }
    

    Please check this Java SE 7 tutorial.

    0 讨论(0)
  • 2020-11-22 01:24

    There are multiple ways to achieve this.

    1. In case of just for existence. It could be file or a directory.

      new File("/path/to/file").exists();
      
    2. Check for file

      File f = new File("/path/to/file"); 
        if(f.exists() && f.isFile()) {}
      
    3. Check for Directory.

      File f = new File("/path/to/file"); 
        if(f.exists() && f.isDirectory()) {}
      
    4. Java 7 way.

      Path path = Paths.get("/path/to/file");
      Files.exists(path)  // Existence 
      Files.isDirectory(path)  // is Directory
      Files.isRegularFile(path)  // Regular file 
      Files.isSymbolicLink(path)  // Symbolic Link
      
    0 讨论(0)
  • 2020-11-22 01:25

    Don't. Just catch the FileNotFoundException. The file system has to test whether the file exists anyway. There is no point in doing all that twice, and several reasons not to, such as:

    • double the code
    • the timing window problem whereby the file might exist when you test but not when you open, or vice versa, and
    • the fact that, as the existence of this question shows, you might make the wrong test and get the wrong answer.

    Don't try to second-guess the system. It knows. And don't try to predict the future. In general the best way to test whether any resource is available is just to try to use it.

    0 讨论(0)
  • 2020-11-22 01:27

    I would recommend using isFile() instead of exists(). Most of the time you are looking to check if the path points to a file not only that it exists. Remember that exists() will return true if your path points to a directory.

    new File("path/to/file.txt").isFile();
    

    new File("C:/").exists() will return true but will not allow you to open and read from it as a file.

    0 讨论(0)
提交回复
热议问题