英文:
Unexpected behavior for Files.isWritable
问题
Files.isWritable
在 Windows 计算机上对于位于网络上的文件(无论是直接访问还是通过映射驱动器访问)始终返回 false。
然而:
- 文件具有写入权限。
FileUtils.writeStringToFile
可以成功地写入此文件而不会出现任何错误。(https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile-java.io.File-java.lang.String-java.lang.String-)
是否有更好的 API 可以在网络上针对文件提供更可靠的结果/权限?
英文:
Files.isWritable
on Windows machine is always giving false in case of a file located on network (both when accessing directly or through mapped drive).
However:
- File has write permission.
FileUtils.writeStringToFile
successfully writes to this file without giving any error. (https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeStringToFile-java.io.File-java.lang.String-java.lang.String-)
Is there a better API which can give more reliable results / permissions for file over network?
答案1
得分: 2
我建议您升级JDK(Java开发工具包)如果您有这个能力的话。关于Files.isWritable
已经有多个bug被报告了,比如这个和相关的bug,其中一个可能是您问题的原因。
我在使用JDK11时注意到类似的问题,并为我的特定情况添加了这个临时解决方法。当我升级到JDK14/15后,我移除了这个解决方法:
public static boolean isWritable(Path path)
{
boolean writable = Files.isWritable(path);
if (!writable)
{
writable = path.toFile().canWrite();
}
return writable;
}
英文:
I suggest you upgrade JDK if you are able to. There have been several bugs reported with Files.isWritable
- such as this and the related bugs, one of which may be the cause of your issue.
I noted similar problems when using JDK11 and added this temporary workaround for my particular case. I removed the workaround once I upgraded to JDK14/15:
public static boolean isWritable(Path path)
{
boolean writable = Files.isWritable(path);
if (!writable)
{
writable = path.toFile().canWrite();
}
return writable;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论