In Java, Files.getOwner(path) returns the owner of a file as a UserPrincipal. Call getName() for its name, such as alice on Linux or DESKTOP-1\alice on Windows.
Two principals for the same account are equals, so you can check "is this file owned by the same user as that one?" without comparing name strings.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.getOwner(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Path, and optionally NOFOLLOW_LINKS:
Java
UserPrincipal owner = Files.getOwner(path);
String name = owner.getName();
Call
What it does
owner.getName()
The account name.
owner.equals(other)
Same account?
Files.getAttribute(p, "owner:owner")
The same principal by attribute name.
Since Java 7. On Windows, files created by an elevated administrator are often owned by the Administrators group rather than the user.
Library declarations
Java
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException
Example 1 — Compare a File's Owner with Its Folder's
Files you create belong to you, so a new file and its new folder have the same owner.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;
public class GetOwnerBasic {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("mine.txt"), new byte[0]);
UserPrincipal owner = Files.getOwner(file);
System.out.println("Owner has a name: " + !owner.getName().isEmpty());
System.out.println("Same owner as the folder: " + owner.equals(Files.getOwner(dir)));
Files.delete(file);
Files.delete(dir);
}
}
Output
Run with java GetOwnerBasic. The output is the same on Windows, macOS, and Linux; the owner's real name depends on who runs the program.
Owner has a name: true
Same owner as the folder: true
How It Works
A principal, not a string.getOwner returns a UserPrincipal; getName() gives its text form.
Your account. The program created both the folder and the file, so both belong to the account running it.
Name not printed. The actual name differs on every machine, so the example prints checks instead. Add System.out.println(owner.getName()) to see yours.
Example 2 — getOwner, the Owner View, and owner:owner
Read the owner through the attribute view and by attribute name, and confirm all three agree.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.UserPrincipal;
public class GetOwnerView {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("a.txt"), new byte[0]);
UserPrincipal direct = Files.getOwner(file);
FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
Object byName = Files.getAttribute(file, "owner:owner");
System.out.println("View agrees: " + direct.equals(view.getOwner()));
System.out.println("owner:owner agrees: " + direct.equals(byName));
System.out.println("owner:owner is a UserPrincipal: " + (byName instanceof UserPrincipal));
Files.delete(file);
Files.delete(dir);
}
}
Output
Run with java GetOwnerView. The output is the same on Windows, macOS, and Linux.
View agrees: true
owner:owner agrees: true
owner:owner is a UserPrincipal: true
How It Works
The shortcut.Files.getOwner is a convenience over the owner attribute view.
The view.FileOwnerAttributeView offers getOwner() and setOwner() on one object.
By name. The attribute owner:owner returns the same principal typed as Object.
Example 3 — Find the Same Principal from Its Name
UserPrincipalLookupService turns a name back into a principal equal to the owner.
Java
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
public class GetOwnerLookup {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("a.txt"), new byte[0]);
UserPrincipal owner = Files.getOwner(file);
UserPrincipalLookupService lookup = FileSystems.getDefault().getUserPrincipalLookupService();
UserPrincipal found = lookup.lookupPrincipalByName(owner.getName());
System.out.println("Lookup finds the owner: " + found.equals(owner));
Files.delete(file);
Files.delete(dir);
}
}
Output
Run with java GetOwnerLookup. Typical output on Windows, macOS, and Linux; in unusual setups (for example a container user with no name) the lookup can behave differently.
Lookup finds the owner: true
How It Works
From the file system. Each file system provides its own lookup service, here the default one.
Name to principal.lookupPrincipalByName resolves the name through the operating system's user database.
Equal principals. The result identifies the same account, so it equals the owner; this is also the first step before setOwner.
Frequently Asked Questions
Files.getOwner(path).getName().
An object representing a user account on the file system. Compare principals with equals; use getName() for display.
When a program runs elevated, Windows may assign new files to the Administrators group instead of the user.
On Linux and macOS: Files.readAttributes(path, PosixFileAttributes.class).group().
Most do. File systems without owners, such as FAT, throw UnsupportedOperationException.