Java如何判断目录是否存在呢?
下文笔者讲述判断目录是否存在的方法分享,如下所示
判断目录是否存在的实现思路
借助File类的exists()方法检测路径是否存在
并结合isDirectory()方法检测目录是否存在
即可判断一个目录是否存在
例:
import java.io.File;
public class DirectoryExistsExample {
public static void main(String[] args) {
String directoryPath = "D:/path/to/directory";
File directory = new File(directoryPath);
if (directory.exists() && directory.isDirectory()) {
System.out.println("目录存在");
} else {
System.out.println("目录不存在");
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


