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

Java examples for File Path IO:File Owner

Description

Set a File Owner Using FileOwnerAttributeView.setOwner()

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.FileOwnerAttributeView;
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");  
    FileOwnerAttributeView foav = Files.getFileAttributeView(path,   
                                              FileOwnerAttributeView.class);  
   try {  //from w ww .  ja v  a  2 s  . c om
       owner = path.getFileSystem().getUserPrincipalLookupService().  
                                    lookupPrincipalByName("apress");  
       foav.setOwner(owner);  
    } catch (IOException e) {  
       System.err.println(e);  
    }  

  }
}

Result


Related Tutorials