Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.nio.file.FileSystems;
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.EnumSet;
import java.util.List;
import java.util.Set;

public class Main {
    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;
        }
        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);
    }
}