Java Files getOwner() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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();
CallWhat 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

Parameters

ParameterDescription
pathThe file or folder.
optionsNOFOLLOW_LINKS for a link's own owner.

Return Value

Return ValueDescription
UserPrincipalThe owner.

Compare with Similar Methods

Ownership methods:

MethodHow it differs
setOwner()Change the owner.
getPosixFilePermissions()What the owner may do (Linux, macOS).
getAttribute()Read owner:owner by name.

Java Examples

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);
    }
}

How It Works

  1. A principal, not a string. getOwner returns a UserPrincipal; getName() gives its text form.
  2. Your account. The program created both the folder and the file, so both belong to the account running it.
  3. 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);
    }
}

How It Works

  1. The shortcut. Files.getOwner is a convenience over the owner attribute view.
  2. The view. FileOwnerAttributeView offers getOwner() and setOwner() on one object.
  3. 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);
    }
}

How It Works

  1. From the file system. Each file system provides its own lookup service, here the default one.
  2. Name to principal. lookupPrincipalByName resolves the name through the operating system's user database.
  3. 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.

Next: Files.getPosixFilePermissions()

Read a file's Unix permission bits as a Set of PosixFilePermission (Linux, macOS).

getPosixFilePermissions() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful