Get a File Owner Using Files.getAttribute() - Java File Path IO

Java examples for File Path IO:File Owner

Description

Get a File Owner Using Files.getAttribute()

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
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) {

    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");  
    try {              
        UserPrincipal owner = (UserPrincipal) Files.getAttribute(path,   
                                                   "owner:owner",LinkOption.NOFOLLOW_LINKS);  
        System.out.println(owner.getName());  
        } catch (IOException e) {  
            System.err.println(e);  
        }  //from   w  ww  .  java 2 s.com

  }
}

Result

The file owner attribute can be required with the name: owner.

The generally accepted form is [view-name:]attribute-name.

The view-name is owner.


Related Tutorials