Example usage for java.nio.file.attribute PosixFileAttributeView setOwner

List of usage examples for java.nio.file.attribute PosixFileAttributeView setOwner

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFileAttributeView setOwner.

Prototype

void setOwner(UserPrincipal owner) throws IOException;

Source Link

Document

Updates the file owner.

Usage

From source file:Test.java

private static void setGroupPrincipal(Path path, String userName, String groupName) throws Exception {
    System.out.println("Setting owner for " + path.getFileName());
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();
    System.out.println("Old Group: " + attributes.group().getName());
    System.out.println("Old Owner: " + attributes.owner().getName());

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(userName);
    GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(groupName);
    view.setGroup(groupPrincipal);/*  w  ww.ja v  a  2s .  c om*/
    view.setOwner(userPrincipal);

    attributes = view.readAttributes();
    System.out.println("New Group: " + attributes.group().getName());
    System.out.println("New Owner: " + attributes.owner().getName());
}