Example usage for java.nio.file.attribute AclEntryType ALLOW

List of usage examples for java.nio.file.attribute AclEntryType ALLOW

Introduction

In this page you can find the example usage for java.nio.file.attribute AclEntryType ALLOW.

Prototype

AclEntryType ALLOW

To view the source code for java.nio.file.attribute AclEntryType ALLOW.

Click Source Link

Document

Explicitly grants access to a file or directory.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    AclFileAttributeView aclView = Files.getFileAttributeView(path, AclFileAttributeView.class);
    if (aclView == null) {
        System.out.format("ACL view  is not  supported.%n");
        return;/*  w w  w.  j a  va 2 s  .c  o m*/
    }
    UserPrincipal bRiceUser = FileSystems.getDefault().getUserPrincipalLookupService()
            .lookupPrincipalByName("brice");

    Set<AclEntryPermission> permissions = EnumSet.of(AclEntryPermission.READ_DATA,
            AclEntryPermission.WRITE_DATA);

    AclEntry.Builder builder = AclEntry.newBuilder();
    builder.setPrincipal(bRiceUser);
    builder.setType(AclEntryType.ALLOW);
    builder.setPermissions(permissions);
    AclEntry newEntry = builder.build();

    List<AclEntry> aclEntries = aclView.getAcl();

    aclEntries.add(newEntry);

    aclView.setAcl(aclEntries);
}