集册 Java实例教程 使用AclFileAttributeView维护文件的ACL

使用AclFileAttributeView维护文件的ACL

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

558
使用AclFileAttributeView维护文件的ACL
/*来自 N  o w  J a v a . c o m*/

import java.io.IOException;

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.AclEntryFlag;

import java.nio.file.attribute.AclEntryPermission;

import java.nio.file.attribute.AclFileAttributeView;

import java.util.List;

import java.util.Set;


public class Main {

  public static void main(String[] args) {

    Path path = Paths.get("C:/home/docs/users.txt");

    try {

      AclFileAttributeView view = Files.getFileAttributeView(path,

          AclFileAttributeView.class);

      List<AclEntry> aclEntryList = view.getAcl();//from N o  w  J a v a . c o m - 时  代  Java

      for (AclEntry entry : aclEntryList) {

        System.out.println("User Principal Name: "

            + entry.principal().getName());

        System.out.println("ACL Entry Type: " + entry.type());

        displayEntryFlags(entry.flags());

        displayPermissions(entry.permissions());

        System.out.println();

      }

    } catch (IOException e) {

      e.printStackTrace();

    }

  }


  private static void displayPermissions(Set<AclEntryPermission> permissionSet) {

    if (permissionSet.isEmpty()) {

      System.out.println("No Permissions present");

    } else {

      System.out.println("Permissions");

      for (AclEntryPermission permission : permissionSet) {

        System.out.print(permission.name() + " ");

      }

      System.out.println();

    }

  }


  private static void displayEntryFlags(Set<AclEntryFlag> fla
展开阅读全文