Java 为什么即使目录设置为只读,Files.iswriteable()方法也会返回true?
我已经通过Java 为什么即使目录设置为只读,Files.iswriteable()方法也会返回true?,java,Java,我已经通过setReadOnly方法以及setWritable(false)明确设置了只读模式 我还通过windows资源管理器检查了指定的目录是否标记为只读: 在上面的示例中,文件.canWrite返回true。是否有更严格的方式强制执行只读模式?api规范将以下内容作为免责声明- 文件系统可能对服务器上的某些操作实施限制 实际的文件系统对象,例如读取、写入和执行。 这些限制统称为访问权限。这个 文件系统可能在单个服务器上具有多组访问权限 对象例如,一个集合可能应用于对象的所有者,并且 另一
setReadOnly
方法以及setWritable(false)
明确设置了只读模式
我还通过windows资源管理器检查了指定的目录是否标记为只读:
在上面的示例中,文件.canWrite
返回true。是否有更严格的方式强制执行只读模式?api规范将以下内容作为免责声明-
文件系统可能对服务器上的某些操作实施限制
实际的文件系统对象,例如读取、写入和执行。
这些限制统称为访问权限。这个
文件系统可能在单个服务器上具有多组访问权限
对象例如,一个集合可能应用于对象的所有者,并且
另一种可能适用于所有其他用户。服务器上的访问权限
对象可能会导致此类中的某些方法失败
你肯定就是这样。在Linux(Ubuntu-18.04)上,事情看起来很正常,符合规范。
下面打印了在2种环境中使用Java sdk-1.8.0_222的以下代码的输出
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try {
// create new file
f = new File("test");
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
f.setWritable(true);
bool = f.canWrite();
System.out.println("Can write to test: "+bool+"\n\n");
f.mkdir();
//f.createNewFile();
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
Windows(文件)-
Linux(文件)-
Windows(目录)-
Linux(目录)-
现在,您可以观察到,对于文件或目录,如果它不存在,canWrite()
总是False
。
在Linux平台上创建文件/目录后,无论是文件还是目录,设置可写都可以无缝工作。然而,在Windows框中,目录似乎返回true。这就是我在上面几行中引用的行为。但是你真的能写下来吗?也就是说,在其中创建或删除一个文件?这与报告错误值的
canWrite()
不同。“只读(仅适用于文件夹中的文件)”?@user207421问题在于我正在使用第三方库(sleepycat Berkeley db),该库检查file.canWrite,如果为真,则引发异常。因为我不能更改它的代码,所以我唯一的选择就是通过我的代码正确设置只读标志。这是一个网络文件夹吗?第三方库是否需要一个目录路径或没有任何特定内容?感谢提供详细信息。你知道有更严格的方法来执行这些限制吗?
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try {
// create new file
f = new File("test");
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
f.setWritable(true);
bool = f.canWrite();
System.out.println("Can write to test: "+bool+"\n\n");
f.mkdir();
//f.createNewFile();
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
f.setWritable(false);
bool = f.canWrite();
System.out.println("Can write to test: "+bool);
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
C:\Users\rranjan\Desktop>java FileDemo
Can write to test: false
Can write to test: false
Can write to test: false
Can write to test: true
C:\Users\rranjan\Desktop>java FileDemo
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Can write to test: false
Can write to test: true
Can write to test: false
Can write to test: true
rranjan@my-workstation:~/scratch/java$ java FileDemo
Can write to test: false
Can write to test: false
Can write to test: false
Can write to test: true
rranjan@my-workstation:~/scratch/java$ java FileDemo
Can write to test: false
Can write to test: true
Can write to test: false
Can write to test: true
C:\Users\rranjan\Desktop>java FileDemo
Can write to test: false
Can write to test: false
Can write to test: true
Can write to test: true
C:\Users\rranjan\Desktop>java FileDemo
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Can write to test: true
Can write to test: true
Can write to test: true
Can write to test: true
rranjan@my-workstation:~/scratch/java$ java FileDemo
Can write to test: false
Can write to test: false
Can write to test: false
Can write to test: true
rranjan@my-workstation:~/scratch/java$ java FileDemo
Can write to test: false
Can write to test: true
Can write to test: false
Can write to test: true