Java Files setOwner() Method

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

Definition

In Java, Files.setOwner(path, owner) makes another account the owner of a file, like the Unix chown command. The new owner is a UserPrincipal, which you obtain from the file system's UserPrincipalLookupService.

Giving a file to someone else normally requires root on Linux and macOS, or administrator rights on Windows. Setting the owner to the current owner is allowed and changes nothing.

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.setOwner(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Look up a principal, then set it:

Java
UserPrincipalLookupService lookup = FileSystems.getDefault().getUserPrincipalLookupService();
UserPrincipal bob = lookup.lookupPrincipalByName("bob");
Files.setOwner(path, bob);
CallWhat it does
lookup.lookupPrincipalByName(n)User by name.
lookup.lookupPrincipalByGroupName(n)Group by name (for posix:group).
Files.setAttribute(p, "owner:owner", u)Same change by attribute name.

Since Java 7. Returns the path.

Library declarations
Java
public static Path setOwner(Path path, UserPrincipal owner) throws IOException

Parameters

ParameterDescription
pathThe file to change.
ownerThe new owner.

Return Value

Return ValueDescription
PathThe path you passed in.

Compare with Similar Methods

Ownership methods:

MethodHow it differs
getOwner()Read the owner.
setPosixFilePermissions()Change what users may do.
setAttribute()Also posix:group.

Java Examples

Example 1 — Set the Owner to the Current Owner

Read the owner and set it again: allowed for a normal user, and nothing changes.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;

public class SetOwnerSame {
    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);
        Path result = Files.setOwner(file, owner);
        System.out.println("Returned the same path: " + result.equals(file));
        System.out.println("Owner unchanged: " + owner.equals(Files.getOwner(file)));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Start from the real owner. The file was just created, so it belongs to the account running the program.
  2. No privilege needed. Keeping the same owner needs no special rights, so the call succeeds for a normal user.
  3. Verify. Reading the owner again returns an equal principal. Changing it to another account would need root or administrator rights.

Example 2 — Look Up a User Who Does Not Exist

The lookup service throws before setOwner is ever called.

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;
import java.nio.file.attribute.UserPrincipalNotFoundException;

public class SetOwnerUnknownUser {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("a.txt"), new byte[0]);
        UserPrincipalLookupService lookup = FileSystems.getDefault().getUserPrincipalLookupService();
        try {
            UserPrincipal ghost = lookup.lookupPrincipalByName("no-such-user-42x");
            Files.setOwner(file, ghost);
        } catch (UserPrincipalNotFoundException e) {
            System.out.println("UserPrincipalNotFoundException for " + e.getName());
        }
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Lookup first. setOwner needs a principal, and the name does not match any account.
  2. A specific exception. UserPrincipalNotFoundException is an IOException, and getName() returns the name that failed.
  3. File untouched. The exception happens before setOwner runs, so a.txt keeps its owner.

Example 3 — Copy the Folder's Owner onto a File

Read the owner of one path and apply it to another.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;

public class SetOwnerFromFolder {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("copy.txt"), new byte[0]);
        UserPrincipal folderOwner = Files.getOwner(dir);
        Files.setOwner(file, folderOwner);
        System.out.println("File owner matches folder: " + Files.getOwner(file).equals(folderOwner));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Owner as a value. A principal read from one path can be applied to another.
  2. Attribute form. Files.setAttribute(file, "owner:owner", folderOwner) would do exactly the same.
  3. Real use. After an administrator copies files into a user's folder, this pattern hands them to the folder's owner. Here both already belong to you, so the call succeeds without special rights.

Frequently Asked Questions

Look up the user with FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(name), then call Files.setOwner(path, user).
Only root (Linux, macOS) or an administrator (Windows) may give a file to another user.
The IOException thrown when the lookup service cannot find the given user or group name.
Look up a group with lookupPrincipalByGroupName, then Files.setAttribute(path, "posix:group", group) on Linux or macOS.
No. It changes only who owns the file; permissions stay as they were.

Next: Files.setPosixFilePermissions()

Set a file's Unix permission bits, like chmod (Linux, macOS).

setPosixFilePermissions() 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.

14 people found this page helpful