简体   繁体   English

Java:unmark文件是只读的

[英]Java: unmark file is read-only

Can I do this on Java? 我可以在Java上这样做吗? I'm using windows... 我正在使用Windows ...

http://java.sun.com/j2se/1.6.0/docs/api/java/io/File.html#setReadOnly%28%29 http://java.sun.com/j2se/1.6.0/docs/api/java/io/File.html#setReadOnly%28%29

File file = new File("foo.bar");
if(file.setReadOnly()) {
    System.out.println("Successful");
}
else {
    System.out.println("All aboard the fail train.");
}

Before Java6, you could not undo this. 在Java6之前,您无法撤消此操作。 To get around this, they put in File.setWritable(boolean) which can be used as so 为了解决这个问题,他们放入了File.setWritable(boolean) ,可以这样使用

File file = new File("foo.bar");
if(file.setWritable(false)) {
    System.out.println("Successful");
}
else {
    System.out.println("All aboard the fail train.");
}

if(file.setWritable(true)) {
    System.out.println("Re-enabled writing");
}
else {
    System.out.println("Failed to re-enable writing on file.");
}
final File f = new File(...);
f.setWritable(true);

Will change premissions to writable (not read-only). 将提交更改为可写(不是只读)。

Note: this may not work all the times, as the underlying FileSystem may deny the request. 注意:这可能无法一直工作,因为底层文件系统可能会拒绝请求。 But it works on most files on your hard drives. 但它适用于硬盘驱动器上的大多数文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM