Java Files readAttributes() Method

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

Definition

In Java, Files.readAttributes reads several attributes of a file in one operation. That is faster than separate calls and gives a consistent snapshot.

One overload takes a class such as BasicFileAttributes.class and returns a typed object with methods like size() and isDirectory(). The other takes a string like "size,isDirectory" or "basic:*" and returns a Map from names to values.

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

Syntax

Typed object or map:

Java
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
Map<String, Object> map = Files.readAttributes(path, "size,lastModifiedTime");
DosFileAttributes dos = Files.readAttributes(path, DosFileAttributes.class);   // Windows
CallWhat it does
BasicFileAttributes.classSize, times, and type; works everywhere.
DosFileAttributes.classAdds readonly, hidden, system, archive.
PosixFileAttributes.classAdds owner, group, permissions.
"basic:*"Every basic attribute as a map.

Since Java 7. The typed result is a snapshot: it does not change when the file changes later.

Library declarations
Java
public static <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException
public static Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException

Parameters

ParameterDescription
pathThe file to inspect.
typeWhich attributes interface to read.
attributes[view:]name,name or [view:]*.
optionsNOFOLLOW_LINKS to read a link itself.

Return Value

Return ValueDescription
A(Class overload) The attributes object.
Map<String, Object>(String overload) Names without the view prefix, mapped to values.

Compare with Similar Methods

Attribute access:

MethodHow it differs
getAttribute()One attribute by name.
getLastModifiedTime()Just the time, typed.
find()Gives each match's BasicFileAttributes during a search.

Java Examples

Example 1 — Read Type, Size, and Time Together

One call returns an object with all basic attributes.

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

public class ReadAttributesBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("hello.txt"), "Hello, Java!".getBytes(StandardCharsets.UTF_8));
        Files.setLastModifiedTime(file, FileTime.fromMillis(1_000_000_000_000L));
        BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
        System.out.println("Regular file: " + attrs.isRegularFile());
        System.out.println("Directory: " + attrs.isDirectory());
        System.out.println("Size: " + attrs.size());
        System.out.println("Modified: " + attrs.lastModifiedTime());
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. One trip to the file system. All four values come from a single read, instead of four separate Files calls.
  2. Typed getters. size() returns a long and lastModifiedTime() a FileTime, so no casts are needed.
  3. Known values. "Hello, Java!" is 12 bytes, and the time was set to a fixed moment just before reading.

Example 2 — Read Two Attributes into a Map

List the names you want, separated by commas, and print the map in sorted key order.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.TreeMap;

public class ReadAttributesMap {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("data.bin"), new byte[12]);
        Map<String, Object> attrs = Files.readAttributes(file, "basic:size,isDirectory");
        System.out.println(new TreeMap<>(attrs));
        System.out.println("size is a " + attrs.get("size").getClass().getSimpleName());
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Comma list. The view prefix is written once; both names belong to the basic view.
  2. Keys without the prefix. The map uses plain names such as size, not basic:size.
  3. Sorted copy. The returned map has no defined order, so a TreeMap copy prints the keys alphabetically.

Example 3 — List the Names Behind "basic:*"

Ask for all basic attributes and print just their names, sorted.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.TreeSet;

public class ReadAttributesAll {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("a.txt"), new byte[0]);
        Map<String, Object> all = Files.readAttributes(file, "basic:*");
        System.out.println(all.size() + " attributes:");
        for (String name : new TreeSet<>(all.keySet())) {
            System.out.println("  " + name);
        }
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. A wildcard. basic:* requests every attribute of the basic view.
  2. The nine basic names. They match the getters of BasicFileAttributes: three times, size, four type flags, and fileKey.
  3. Names only. The values (times, file key) differ from machine to machine, so only the sorted names are printed. fileKey may be null on Windows.

Frequently Asked Questions

BasicFileAttributes a = Files.readAttributes(path, BasicFileAttributes.class); then use a.size(), a.lastModifiedTime(), a.isDirectory().
The Class overload returns a typed object; the String overload returns a Map<String, Object> of the named attributes.
Yes: Files.readAttributes(path, DosFileAttributes.class) gives isReadOnly(), isHidden(), isSystem(), and isArchive().
Files.readAttributes(path, PosixFileAttributes.class) on Linux and macOS; it throws UnsupportedOperationException on Windows.
No. It is a snapshot taken when readAttributes was called.

Next: Files.readString()

Read a whole text file into one String, UTF-8 by default (Java 11+).

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

9 people found this page helpful