Java Files setAttribute() Method

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

Definition

In Java, Files.setAttribute(path, "view:name", value) changes one attribute of a file. It uses the same names as getAttribute, and the value must have the matching type, for example a FileTime for times or a Boolean for DOS flags.

Only writable attributes can be set: the basic view allows the three timestamps, the dos view (Windows) allows readonly, hidden, system, and archive, and the posix view allows permissions, owner, and group.

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

Syntax

Path, attribute name, and a correctly typed value:

Java
Files.setAttribute(path, "basic:lastModifiedTime", FileTime.fromMillis(ms));
Files.setAttribute(path, "dos:readonly", true);                // Windows
Files.setAttribute(path, "posix:permissions", perms);          // Linux, macOS
CallWhat it does
"basic:lastModifiedTime"Value: FileTime.
"dos:readonly"Value: Boolean (Windows).
"posix:permissions"Value: Set<PosixFilePermission>.

Since Java 7. Returns the path. Prefer typed methods such as setLastModifiedTime when one exists; the compiler then checks the value type.

Library declarations
Java
public static Path setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException

Parameters

ParameterDescription
pathThe file to change.
attribute[view:]name of a writable attribute.
valueNew value of the right type.
optionsNOFOLLOW_LINKS to change a link itself.

Return Value

Return ValueDescription
PathThe path you passed in.

Compare with Similar Methods

Related methods:

MethodHow it differs
getAttribute()Read one attribute by name.
setLastModifiedTime()Typed way to change the time.
setPosixFilePermissions()Typed way to change permissions.

Java Examples

Example 1 — Backdate a File with basic:lastModifiedTime

Set a fixed time through the string-based API and read it back with the typed one.

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

public class SetAttributeTime {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("report.txt"), new byte[0]);
        FileTime when = FileTime.fromMillis(1_000_000_000_000L);
        Path same = Files.setAttribute(file, "basic:lastModifiedTime", when);
        System.out.println("Returned the same path: " + same.equals(file));
        System.out.println("lastModifiedTime: " + Files.getLastModifiedTime(file));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Name plus value. The attribute string picks the timestamp, and the FileTime supplies a fixed moment in 2001.
  2. Chainable result. setAttribute returns its path argument, like most Files methods that change a file.
  3. Read it back. getLastModifiedTime shows the new value in UTC.

Example 2 — Wrong Value Type and a Read-Only Attribute

Passing a long instead of a FileTime, and trying to set the size, both fail.

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

public class SetAttributeMistakes {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("a.txt"), new byte[8]);
        try {
            Files.setAttribute(file, "basic:lastModifiedTime", 1_000_000_000_000L);
        } catch (ClassCastException e) {
            System.out.println("Time as a long: ClassCastException");
        }
        try {
            Files.setAttribute(file, "basic:size", 0L);
        } catch (IllegalArgumentException e) {
            System.out.println("basic:size: IllegalArgumentException");
        }
        System.out.println("Size still: " + Files.size(file));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Types are checked at run time. The value parameter is an Object, so a Long compiles but cannot be cast to FileTime.
  2. Size is read-only. The basic view lets you change only the three timestamps, so basic:size is rejected.
  3. Nothing changed. Both calls failed before touching the file, which still holds 8 bytes. Use truncate on a channel to shorten a file.

Example 3 — Make a File Read-Only on Windows

Set dos:readonly where the DOS view exists alone, check isWritable, and clear the flag again before deleting.

Java
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

public class SetAttributeDosReadOnly {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("locked.txt"), new byte[1]);
        Set<String> views = FileSystems.getDefault().supportedFileAttributeViews();
        if (views.contains("dos") && !views.contains("posix")) {
            Files.setAttribute(file, "dos:readonly", true);
            System.out.println("dos:readonly = " + Files.getAttribute(file, "dos:readonly"));
            System.out.println("isWritable: " + Files.isWritable(file));
            Files.setAttribute(file, "dos:readonly", false);
            System.out.println("Cleared, isWritable: " + Files.isWritable(file));
        } else {
            System.out.println("Not Windows: use POSIX permissions instead");
        }
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Pick the platform. Linux can also offer a dos view (stored in extended attributes), so the test also requires that posix is absent, which means Windows.
  2. The flag matters. On Windows, isWritable checks the DOS read-only flag, so it turns false.
  3. Clear before delete. Windows refuses to delete a read-only file with AccessDeniedException, so the flag is set back to false first.

Frequently Asked Questions

Files.setAttribute(path, "dos:readonly", true). On Linux and macOS, remove the write bits with Files.setPosixFilePermissions instead.
Timestamps in basic; readonly, hidden, system, and archive in dos; permissions, owner, and group in posix; owner in owner.
The value has the wrong type, for example a long where a FileTime is needed.
No. basic:size is read-only; truncate through a channel from Files.newByteChannel.
Yes, by default. Pass LinkOption.NOFOLLOW_LINKS to change the link itself where the system allows it.

Next: Files.setLastModifiedTime()

Change a file's last-modified time to any FileTime.

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

12 people found this page helpful