Example usage for java.nio.file.attribute AclFileAttributeView setAcl

List of usage examples for java.nio.file.attribute AclFileAttributeView setAcl

Introduction

In this page you can find the example usage for java.nio.file.attribute AclFileAttributeView setAcl.

Prototype

void setAcl(List<AclEntry> acl) throws IOException;

Source Link

Document

Updates (replace) the access control list.

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;/*from  w  w w  .  ja va2 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);
}