[在Windows上使用java8 NIO2设置文件权限是否有其他方法吗?
file.setReadable(false, false);
file.setExecutable(false, false);
file.setWritable(false, false);
设置各种属性的File
方法:setExecutable
,setReadable
,setReadOnly
,setWritable
被Files
方法setAttribute(Path, String, Object, LinkOption...)代替。
用法示例:
public void setFileAttributes() throws IOException {
Path path = ...;
UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("user");
AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
AclEntry entry = AclEntry.newBuilder()
.setType(ALLOW)
.setPrincipal(user)
.setPermissions(Set.of(READ_DATA, EXECUTE, WRITE_DATA))
.build();
List<AclEntry> acl = view.getAcl();
acl.add(0, entry);
Files.setAttribute(path, "acl:acl", acl);
}
请参见AclFileAttributeView以获取更多详细信息。