更新时间:2023-11-29 19:38:28
检查目录是否存在。
档案档案=新档案(c:\\cygwin\ \cygwin.bat);
if(!file.isDirectory())
file = file.getParentFile();
if(file.exists()){
...
}
$ b $看起来像file.canWrite()没有给你一个明确的指示,如果你有权限写入目录。
I need to determine if a user-supplied String is a valid file path (i.e. if createNewFile() will succeed or throw an Exception) but i don't want to bloat the file system with useless files created just for validation purposes, so is there a way to determine if the String i have is a valid file path without attempting to create the file?
I know the definition of "valid file path" varies depending on the OS, but i was wondering if there was any quick way of accepting "C:/foo" or "/foo" and rejecting "banana"...a possible approach may be attempting to create the file and eventually deleting it if the creation succeeded, but i hope there is a more elegant way of achieving the same result...
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()){
...
}
It seems like file.canWrite() does not give you a clear indication if you have permissions to write to the directory.