Java Get File Owner Example

In this source code example, we show you how to get an owner of the file in Java.

We use Files.getOwner() method to get an owner of the file in Java.

Java Get File Owner Example

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 JavaGetFileOwner {

    public static void main(String[] args) throws IOException {
        
        Path myPath = Paths.get("src/resources/sample.txt");
        
        UserPrincipal userPrincipal = Files.getOwner(myPath);
        String owner = userPrincipal.getName();
        
        System.out.println(owner);
    }
}

References

https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/nio/file/Files.html

Related Java NIO Files Examples

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course