Files.isWritable的意外行为

huangapple go评论67阅读模式
英文:

Unexpected behavior for Files.isWritable

问题

Files.isWritable 在 Windows 计算机上对于位于网络上的文件(无论是直接访问还是通过映射驱动器访问)始终返回 false。

然而:

  1. 文件具有写入权限。
  2. 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:

  1. File has write permission.
  2. 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;
}

huangapple
  • 本文由 发表于 2020年10月12日 13:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64312307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定