如何在Windows上使用Java NIO2设置文件权限?

问题描述 投票:0回答:1

[在Windows上使用java8 NIO2设置文件权限是否有其他方法吗?

file.setReadable(false, false);
file.setExecutable(false, false);
file.setWritable(false, false);
java file permissions nio2
1个回答
0
投票

设置各种属性的File方法:setExecutablesetReadablesetReadOnlysetWritableFiles方法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以获取更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.