Library contents

Java Files attribute views

Read basic attributes together or address one named attribute through a supported view.

A simple example

Files attribute views

  1. Start with A regular file containing ASCII AB
  2. Do this Measure its length
  3. Result 2 bytes
Each ASCII character occupies one byte and this file has no trailing newline.

Syntax

Java syntax
public static <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
JDK 21 declarations (5)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Build a document catalog entry

An archive imports a small receipt and needs its size and file type for a catalog card. Read the basic metadata as a group, then check the single named size against the value on the card.

First output linetrue
Main.java
import java.nio.file.*;
import java.nio.file.attribute.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path receipt = Files.writeString(desk.resolve("receipt.txt"), "paid");
            var attrs = Files.readAttributes(receipt, BasicFileAttributes.class);
            System.out.println(attrs.isRegularFile());
            System.out.println(attrs.size());
            System.out.println(Files.getAttribute(receipt, "basic:size").equals(attrs.size()));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true
4
true

A single basic attribute and the grouped snapshot agree for this unmodified receipt.

Advertisements

Restore the date recorded on a catalog card

A migration team keeps the original modification timestamp separately from a document's contents. Apply that value through a named attribute and consult the basic view to verify what the disk recorded.

Resulttrue
Main.java
import java.nio.file.*;
import java.nio.file.attribute.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path card = Files.createFile(desk.resolve("card.txt"));
            var stamp = FileTime.fromMillis(1704067200000L);
            Files.setAttribute(card, "basic:lastModifiedTime", stamp);
            var view = Files.getFileAttributeView(card, BasicFileAttributeView.class);
            System.out.println(view.readAttributes().lastModifiedTime().equals(stamp));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true

The whole-second date can be stored exactly on the tested local volume.

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. Read the size field on the saved file. How many bytes did these ASCII characters occupy without a trailing newline?

GivenFile contents “clay”
Worked result4

What to watch for

Attribute names and available views depend on the provider. The basic view defines portable names such as size and lastModifiedTime.

Documentation for Files attribute views