Java Files attribute views
Read basic attributes together or address one named attribute through a supported view.
A simple example
Files attribute views
- Start with A regular file containing ASCII AB
- Do this Measure its length
- Result 2 bytes
Syntax
public static <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
JDK 21 declarations (5)
public static Object getAttribute(Path path, String attribute, LinkOption... options) throws IOExceptionpublic static <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)public static <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOExceptionpublic static Map<String,Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOExceptionpublic static Path setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException
Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaBuild 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.
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);
}
}
}
}
true 4 true
A single basic attribute and the grouped snapshot agree for this unmodified receipt.
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.
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);
}
}
}
}
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?
What to watch for
Attribute names and available views depend on the provider. The basic view defines portable names such as size and lastModifiedTime.