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);
Call
What 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
Run with java SetOwnerUnknownUser. The output is the same on Windows, macOS, and Linux, as long as no account has this made-up name.
UserPrincipalNotFoundException for no-such-user-42x
How It Works
Lookup first.setOwner needs a principal, and the name does not match any account.
A specific exception.UserPrincipalNotFoundException is an IOException, and getName() returns the name that failed.
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);
}
}
Output
Run with java SetOwnerFromFolder. Typical output on Windows, macOS, and Linux for files in your own temp folder.
File owner matches folder: true
How It Works
Owner as a value. A principal read from one path can be applied to another.
Attribute form.Files.setAttribute(file, "owner:owner", folderOwner) would do exactly the same.
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.