Java Files getAttribute() Method

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

Definition

In Java, Files.getAttribute(path, "view:name") reads one attribute of a file, chosen by a string. The view part (basic, dos, posix, unix, owner, ...) says which group of attributes to look in; if you leave it out, basic is used.

The result is an Object: for example basic:size is a Long, basic:isDirectory a Boolean, and basic:lastModifiedTime a FileTime. Cast it to the type you expect.

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

Syntax

Pass the path and the attribute name:

Java
long size = (Long) Files.getAttribute(path, "basic:size");
FileTime t = (FileTime) Files.getAttribute(path, "lastModifiedTime");   // basic: is the default
Object hidden = Files.getAttribute(path, "dos:hidden");                  // Windows
CallWhat it does
"basic:size"Long, the size in bytes.
"basic:isDirectory"Boolean.
"basic:lastModifiedTime"FileTime.
"owner:owner"UserPrincipal.

Since Java 7. Only one attribute per call; * or a comma list throws IllegalArgumentException, so use readAttributes for several.

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

Parameters

ParameterDescription
pathThe file to inspect.
attribute[view:]name, for example basic:size.
optionsNOFOLLOW_LINKS to read a link's own attributes.

Return Value

Return ValueDescription
ObjectThe attribute value; its type depends on the attribute.

Compare with Similar Methods

Attribute readers:

MethodHow it differs
readAttributes()Many attributes in one call.
setAttribute()Change one attribute by name.
size()Typed shortcut for basic:size.

Java Examples

Example 1 — Read basic:size and isDirectory

Print each value together with its runtime type, then cast the size to long.

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

public class GetAttributeTypes {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("data.bin"), new byte[42]);
        Object size = Files.getAttribute(file, "basic:size");
        Object isDir = Files.getAttribute(file, "isDirectory");
        System.out.println("basic:size = " + size + " (" + size.getClass().getSimpleName() + ")");
        System.out.println("isDirectory = " + isDir + " (" + isDir.getClass().getSimpleName() + ")");
        long bytes = (Long) Files.getAttribute(file, "size");
        System.out.println("As a long: " + bytes);
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Boxed values. The attribute map stores objects, so the size arrives as a Long and the flag as a Boolean.
  2. basic is the default view. "isDirectory" and "size" without a prefix mean the same as basic:isDirectory and basic:size.
  3. Cast and unbox. (Long) turns the Object back into a number that unboxes into a long.

Example 2 — Read lastModifiedTime by Name

Set a fixed time, read it back through getAttribute, and compare it with the typed method.

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

public class GetAttributeTime {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("old.txt"), new byte[0]);
        Files.setLastModifiedTime(file, FileTime.fromMillis(1_000_000_000_000L));
        FileTime t = (FileTime) Files.getAttribute(file, "basic:lastModifiedTime");
        System.out.println("lastModifiedTime = " + t);
        System.out.println("Same as getLastModifiedTime: " + t.equals(Files.getLastModifiedTime(file)));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. A known moment. 1,000,000,000,000 milliseconds after 1970 is 9 September 2001, 01:46:40 UTC, a whole second that every file system can store.
  2. FileTime prints in ISO-8601. toString() uses UTC, so the text does not depend on your time zone.
  3. Two routes, one value. The string-based call and getLastModifiedTime read the same attribute.

Example 3 — Unknown Attribute vs Unknown View

A wrong attribute name and a missing view fail with different exceptions.

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

public class GetAttributeErrors {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("a.txt"), new byte[0]);
        String[] names = {"basic:isRegularFile", "basic:colour", "nosuchview:size"};
        for (String name : names) {
            try {
                System.out.println(name + " -> " + Files.getAttribute(file, name));
            } catch (IllegalArgumentException e) {
                System.out.println(name + " -> IllegalArgumentException");
            } catch (UnsupportedOperationException e) {
                System.out.println(name + " -> UnsupportedOperationException");
            }
        }
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Valid name. isRegularFile exists in the basic view and is true for a.txt.
  2. Right view, wrong name. basic has no colour attribute, so the name is rejected with IllegalArgumentException.
  3. No such view. An unavailable view gives UnsupportedOperationException, the same thing posix:permissions does on Windows.

Frequently Asked Questions

An Object whose type depends on the attribute: Long for basic:size, Boolean for flags, FileTime for times.
basic everywhere; dos on Windows; posix and unix on Linux and macOS; owner and user on most systems; acl on Windows.
No. Use Files.readAttributes(path, "size,lastModifiedTime") for that.
The view is not supported on this file system, such as posix: on Windows.
Files.getAttribute(path, "dos:hidden") returns a Boolean; Files.isHidden(path) is the portable shortcut.

Next: Files.getFileStore()

Get the FileStore (drive, partition, or volume) that holds a file.

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

10 people found this page helpful