我想在Windows上用Java创建一个只读目录,这样我就无法在里面创建任何目录。我有这个代码,应该在Files.createDirectory(directory);
public static void main(String[] args) throws IOException {
var foo = Paths.get("foo");
var directory = Paths.get("foo/bar");
Files.createDirectory(foo);
var view = Files.getFileAttributeView(foo, AclFileAttributeView.class);
var entries = new ArrayList<AclEntry>();
for (var acl : view.getAcl()) {
var perms = new LinkedHashSet<>(acl.permissions());
perms.remove(AclEntryPermission.WRITE_DATA);
perms.remove(AclEntryPermission.APPEND_DATA);
perms.remove(AclEntryPermission.ADD_SUBDIRECTORY);
var ae = AclEntry.newBuilder()
.setType(acl.type())
.setPrincipal(acl.principal())
.setPermissions(perms)
.setFlags(acl.flags())
.build();
entries.add(ae);
}
view.setAcl(entries);
Files.createDirectory(directory);
}
但是,它没有问题就过去了。当我在里面创建文件时,它会按预期失败。但我仍然可以创建一个目录。如何创建真正的只读目录?