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
- Java File Copy Example
- Java Create File Example
- Java Delete File Example
- Java File Size Example
- Java Get File Owner Example
- Java File Move Example
- Java Read File Example
- Java Write File Example
- Java Create Directory Example
- Java Create Multiple Directories Example
- Java List All Subdirectories From a Directory Example
- Java Append to File Example