且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 中有没有办法在不尝试创建文件的情况下确定路径是否有效?

更新时间:2022-04-15 07:52:46

这也会检查目录是否存在.

This would check for the existance of the directory as well.

File file = new File("c:\cygwin\cygwin.bat");
if (!file.isDirectory())
   file = file.getParentFile();
if (file.exists()){
    ...
}

似乎 file.canWrite() 没有明确指示您是否有权写入目录.

It seems like file.canWrite() does not give you a clear indication if you have permissions to write to the directory.