简体   繁体   English

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

[英]How to set file permissions using java NIO2 on Windows?

Are there any ways of setting file permissions using java8 NIO2 on Windows different from this?有什么方法可以在 Windows 上使用 java8 NIO2 设置文件权限与此不同?

file.setReadable(false, false);
file.setExecutable(false, false);
file.setWritable(false, false);

The File methods that set various attributes: setExecutable , setReadable , setReadOnly , setWritable are replaced by the Files method setAttribute(Path, String, Object, LinkOption...) .设置各种属性的File方法: setExecutablesetReadablesetReadOnlysetWritableFiles方法setAttribute(Path, String, Object, LinkOption...) 替换

Usage Example:使用示例:

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);
}

See AclFileAttributeView for more details.有关详细信息,请参阅AclFileAttributeView

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

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