Java AclFileAttributeView get from Path

Description

Java AclFileAttributeView get from Path

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.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("Main.java");

    AclFileAttributeView aclView = Files.getFileAttributeView(path,
        AclFileAttributeView.class);
    if (aclView == null) {
      System.out.println("ACL view is not supported.");
      return;/*from  ww w . j  a va2s .  c o m*/
    }

    try {
      List<AclEntry> aclEntries = aclView.getAcl();
      for (AclEntry entry : aclEntries) {
        System.out.format("Principal: %s%n", entry.principal());
        System.out.format("Type: %s%n", entry.type());
        System.out.format("Permissions are:%n");

        Set<AclEntryPermission> permissions = entry.permissions();
        for (AclEntryPermission p : permissions) {
          System.out.format("%s %n", p);
        }

      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related