集册 Java实例教程 在ACL中授予新访问权限

在ACL中授予新访问权限

欢马劈雪     最近更新时间:2020-01-02 10:19:05

583
在ACL中授予新访问权限

import java.io.IOException;/* from n o w j a v a . c o m - 时代Java*/

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.AclEntry;

import java.nio.file.attribute.AclEntryPermission;

import java.nio.file.attribute.AclEntryType;

import java.nio.file.attribute.AclFileAttributeView;

import java.nio.file.attribute.UserPrincipal;

import java.util.List;


public class Main {

  public static void main(String[] args) {

    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");

    try {

      UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService()

          .lookupPrincipalByName("aprees");

      AclFileAttributeView view = Files.getFileAttributeView(path,/**来 自 NowJava.com - 时  代  Java**/

          AclFileAttributeView.class);

      AclEntry entry = AclEntry

          .newBuilder()

          .setType(AclEntryType.ALLOW)

          .setPrincipal(user)

          .setPermissions(AclEntryPermission.READ_DATA,

              AclEntryPermission.APPEND_DATA).build();


      // read ACL

      List<AclEntry> acl = view.getAcl();


      
展开阅读全文