Is there a proper way to check for file/directory existence in Java?

后端 未结 4 1856
不思量自难忘°
不思量自难忘° 2021-01-12 23:15

Code:

String dir = //Path to the  directory
File saveDir = new File(dir);
//Here comes the existence check
if(!saveDir.exists())
  saveDir.         


        
4条回答
  •  太阳男子
    2021-01-12 23:59

    new File(dir).getCanonicalFile().isDirectory();  
    

    Skeleton for your reference:-

    File f = new File("....");
    if (!f.exists()) {
        // The directory does not exist.
        ...
    } else if (!f.isDirectory()) {
        // It is not a directory (i.e. it is a file).
        ... 
    }
    

提交回复
热议问题