Home Java Files Methods getAttribute() Java Files getAttribute() Method 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:
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 Call What 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 public static Object getAttribute(Path path, String attribute, LinkOption... options) throws IOException Parameters Parameter Description pathThe file to inspect. attribute[view:]name, for example basic:size.optionsNOFOLLOW_LINKS to read a link's own attributes.
Return Value Return Value Description ObjectThe attribute value; its type depends on the attribute.
Important
Throws IllegalArgumentException for an unknown attribute name, UnsupportedOperationException if the view is not available (for example posix: on Windows or dos: on macOS), and NoSuchFileException if the file is missing. A wrong cast fails with ClassCastException.
Compare with Similar Methods Attribute readers:
Java Examples 1. Values and their types 2. A time attribute 3. Bad names and views Example 1 — Read basic:size and isDirectory Print each value together with its runtime type, then cast the size to long.
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);
}
} Run with java GetAttributeTypes. The output is the same on Windows, macOS, and Linux.
basic:size = 42 (Long)
isDirectory = false (Boolean)
As a long: 42 How It Works Boxed values. The attribute map stores objects, so the size arrives as a Long and the flag as a Boolean.basic is the default view. "isDirectory" and "size" without a prefix mean the same as basic:isDirectory and basic:size.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.
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);
}
} Run with java GetAttributeTime. The time is a fixed value set by the program, so the output is the same on Windows, macOS, and Linux.
lastModifiedTime = 2001-09-09T01:46:40Z
Same as getLastModifiedTime: true How It Works 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.FileTime prints in ISO-8601. toString() uses UTC, so the text does not depend on your time zone.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.
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);
}
} Run with java GetAttributeErrors. The output is the same on Windows, macOS, and Linux.
basic:isRegularFile -> true
basic:colour -> IllegalArgumentException
nosuchview:size -> UnsupportedOperationException How It Works Valid name. isRegularFile exists in the basic view and is true for a.txt.Right view, wrong name. basic has no colour attribute, so the name is rejected with IllegalArgumentException.No such view. An unavailable view gives UnsupportedOperationException, the same thing posix:permissions does on Windows.Frequently Asked Questions What does Files.getAttribute return? An Object whose type depends on the attribute: Long for basic:size, Boolean for flags, FileTime for times.
What attribute views exist? basic everywhere; dos on Windows; posix and unix on Linux and macOS; owner and user on most systems; acl on Windows.
Can getAttribute read several attributes at once? No. Use Files.readAttributes(path, "size,lastModifiedTime") for that.
Why does getAttribute throw UnsupportedOperationException? The view is not supported on this file system, such as posix: on Windows.
How do I check whether a file is hidden on Windows by name? Files.getAttribute(path, "dos:hidden") returns a Boolean; Files.isHidden(path) is the portable shortcut.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion