Java:unmark文件是只读的

Zom*_*ies 9 java windows

我可以在Java上这样做吗?我正在使用Windows ...

cor*_*iKa 10

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.");
}
Run Code Online (Sandbox Code Playgroud)

在Java6之前,您无法撤消此操作.为了解决这个问题,他们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.");
}
Run Code Online (Sandbox Code Playgroud)