Java Files setPosixFilePermissions() Method

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

Definition

In Java, Files.setPosixFilePermissions(path, perms) replaces all nine permission bits of a file, like chmod. Build the set with PosixFilePermissions.fromString("rwxr-xr-x") or from individual PosixFilePermission constants.

Unlike permissions passed when creating a file, this call is not reduced by the umask: you get exactly the bits you ask for. On Windows it throws UnsupportedOperationException.

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

Syntax

Path and a set of permissions:

Java
Files.setPosixFilePermissions(script, PosixFilePermissions.fromString("rwxr-xr-x"));
Files.setPosixFilePermissions(secret, EnumSet.of(OWNER_READ, OWNER_WRITE));   // rw-------
CallWhat it does
fromString("rw-r--r--")644: owner writes, all read.
fromString("rwxr-xr-x")755: runnable by all.
fromString("rw-------")600: private to the owner.

Since Java 7. Only the owner (or root) may change permissions. Returns the path.

Library declarations
Java
public static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) throws IOException

Parameters

ParameterDescription
pathThe file or folder.
permsThe complete new set of permissions.

Return Value

Return ValueDescription
PathThe path you passed in.

Compare with Similar Methods

Permission methods:

MethodHow it differs
getPosixFilePermissions()Read the current bits.
isExecutable()Check the effect on this process.
createFile()Accepts permissions as an initial attribute.

Java Examples

Example 1 — chmod 755 a Script

Set rwxr-xr-x and confirm that the file is now executable.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;

public class SetPosixPermsExecutable {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path script = Files.write(dir.resolve("run.sh"), "echo hi\n".getBytes(StandardCharsets.UTF_8));
        try {
            Files.setPosixFilePermissions(script, PosixFilePermissions.fromString("rwxr-xr-x"));
            System.out.println("Now: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(script)));
            System.out.println("Executable: " + Files.isExecutable(script));
        } catch (UnsupportedOperationException e) {
            System.out.println("UnsupportedOperationException: not a POSIX file system");
        }
        Files.delete(script);
        Files.delete(dir);
    }
}

How It Works

  1. Text to set. fromString parses the nine letters; a dash means the bit is off.
  2. Exact result. Reading back shows rwxr-xr-x, because this call is not affected by the umask.
  3. The effect. With the owner's x bit set, isExecutable returns true and ./run.sh could be started from a shell.

Example 2 — Add One Permission and Remove Another

Copy the current set, edit it, and write it back.

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.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
import java.util.Set;

public class SetPosixPermsEdit {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("tool"), new byte[0]);
        if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
            Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-r--r--"));
            Set<PosixFilePermission> perms = EnumSet.copyOf(Files.getPosixFilePermissions(file));
            perms.add(PosixFilePermission.OWNER_EXECUTE);
            perms.remove(PosixFilePermission.OTHERS_READ);
            Files.setPosixFilePermissions(file, perms);
            System.out.println("Now: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(file)));
        } else {
            System.out.println("No POSIX permissions on this system");
        }
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Like chmod u+x,o-r. There is no "add one bit" method, so read the set, change it, and set it again.
  2. A modifiable copy. EnumSet.copyOf guarantees a set you may change, whatever the file system returned.
  3. Result. rw-r--r-- plus the owner's x and minus others' r gives rwxr-----.

Example 3 — Permissions at Creation Are Reduced by the umask

Ask for rw-rw-rw- when creating a file, then set the same bits explicitly.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class SetPosixPermsUmask {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Set<PosixFilePermission> wanted = PosixFilePermissions.fromString("rw-rw-rw-");
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(wanted);
        try {
            Path shared = Files.createFile(dir.resolve("shared.txt"), attr);
            System.out.println("Created as: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(shared)));
            Files.setPosixFilePermissions(shared, wanted);
            System.out.println("After set: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(shared)));
            Files.delete(shared);
        } catch (UnsupportedOperationException e) {
            System.out.println("POSIX initial attribute: UnsupportedOperationException");
        }
        Files.delete(dir);
    }
}

How It Works

  1. Requested at creation. asFileAttribute passes rw-rw-rw- to createFile, but the operating system removes the bits in the umask.
  2. The usual umask, 022. It clears group and others' write, so the file is created as rw-r--r--. With umask 002 it would be rw-rw-r--.
  3. Explicit set wins. setPosixFilePermissions is not filtered, so the file ends up exactly rw-rw-rw-.

Frequently Asked Questions

Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x")) on Linux or macOS.
On Linux and macOS set an x bit as above; path.toFile().setExecutable(true) also works and does nothing harmful on Windows.
The umask removes bits at creation time. Call setPosixFilePermissions afterwards to get the exact bits.
No, it throws UnsupportedOperationException. Use dos:readonly or ACLs on Windows.
Its owner and root. Other users get FileSystemException or AccessDeniedException.

Next: Files.size()

Get the size of a file in bytes as a long.

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

15 people found this page helpful