Set a File Owner Using Files.setOwner() - Java File Path IO

Java examples for File Path IO:File Owner

Introduction

You can set a file owner by calling the Files.setOwner() method.

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;

public class Main {
  public static void main(String[] args) {
    UserPrincipal owner = null;  
    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");  
    try {  /* w  ww . j av a2 s. c  o m*/
        owner = path.getFileSystem().getUserPrincipalLookupService().  
                                           lookupPrincipalByName("apress");  
        Files.setOwner(path, owner);  
    } catch (IOException e) {  
        System.err.println(e);  
    } 
  }
}

Result


Related Tutorials