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

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

Introduction

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

Prototype

void setOwner(UserPrincipal owner) throws IOException;

Source Link

Document

Updates the file owner.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");

    view.setOwner(userPrincipal);
    System.out.println("Owner: " + view.getOwner().getName());
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipal userPrincipal = view.getOwner();

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    userPrincipal = lookupService.lookupPrincipalByName("users");
    view.setOwner(userPrincipal);
    System.out.println("UserPrincipal set: " + userPrincipal.getName());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);

    UserPrincipal owner = foav.getOwner();
    System.out.format("Original owner  of  %s  is %s%n", path, owner.getName());

    FileSystem fs = FileSystems.getDefault();
    UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();

    UserPrincipal newOwner = upls.lookupPrincipalByName("brice");
    foav.setOwner(newOwner);

    UserPrincipal changedOwner = foav.getOwner();
    System.out.format("New owner  of  %s  is %s%n", path, changedOwner.getName());

}