Java Programming Tutorials

Java programming tutorials with many code examples!

Java File Metadata/Attributes


How to read file metadata in Java? In this post we’ll show three ways to do that – using modern java.nio.file.Files API and legacy version using

Basically, there are three ways to get information about files:

  1. Using legacy API
  2. Using NIO Files to get metadata one by one.
  3. Using NIO Files to get all metadata at once.

Of course, the File version works fine in modern Java and sometimes is enough, especially when you already have File object and want to read the most common attributes (is file/directory/executable/readable/writable/size).

In the new code (since Java 7) it is preferred to use NIO Files and Path to work with files. This part of JDK has comprehensive support for files, file systems, paths, and supports working with different environments (Linux/Windows/Mac).


In the following code we’re going to show how to get file attributes in these three ways:


import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
import java.time.Instant;

import static java.lang.System.out;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;

public class FileMetadataExample {

    public static void main(String[] args) throws IOException {
        Path file = Paths.get("/tmp");




    private static void getAttributesInLegacyWay(Path path)
            throws IOException {
        File file = path.toFile();
        out.printf("Pre Java 7 metadata of '%s':%n",
        out.println("directory: " + file.isDirectory());
        out.println("hidden   : " + file.isHidden());
        out.println("regular  : " + file.isFile());
        out.println("symlink  : " +
        out.println("size     : " + file.length());
        out.println("modification time: "
                // Instant point in time from Java 8:
                + Instant.ofEpochMilli(file.lastModified()));

    private static void getAttributesOneByOne(Path file)
            throws IOException {
        out.printf("%nMetadata of '%s':%n", file.toString());
        out.println("directory: " + Files.isDirectory(file));
        out.println("hidden   : " + Files.isHidden(file));
        // follows symlinks by default:
        out.println("regular  : "
                + Files.isRegularFile(file, NOFOLLOW_LINKS));
        out.println("is same: : " + Files.isSameFile(file, file));
        out.println("symlink  : " + Files.isSymbolicLink(file));
        out.println("size     : " + Files.size(file));
        out.println("owner    : " + Files.getOwner(file).getName());
        out.println("modification time: "
                + Files.getLastModifiedTime(file));

    private static void getAttributesInBulkWay(Path file)
            throws IOException {
        out.printf("%nBasic attributes of '%s':%n",
        PosixFileAttributes attrs = Files.readAttributes(
                file, PosixFileAttributes.class);
        out.println("directory: " + attrs.isDirectory());
        out.println("is other : " + attrs.isOther());
        out.println("regular  : " + attrs.isRegularFile());
        out.println("symlink  : " + attrs.isSymbolicLink());
        out.println("size     : " + attrs.size());
        out.println("unique id: " + attrs.fileKey());
        out.println("access time  : " + attrs.lastAccessTime());
        out.println("creation time: " + attrs.creationTime());
        out.println("modified time: " + attrs.lastModifiedTime());
        out.println("owner: " + attrs.owner());
        out.println("group: " +;

Note that in the last method we can even read owner and group to which the file belongs – common properties in Linux file systems. What’s more, we’ve got access to last access time, creation time, and even file’s unique ID, which is inode in Linux ext file systems. Nice. :-)

When reading all attributes at once we’ve chosen PosixFileAttributes, because we work on Linux. Of course not all information are available on all file systems. In such case they usually be null. Here are other choices of attribute views:

  • BasicFileAttributeView
    Common attributes for all supported file system implementations.
  • DosFileAttributeView
    Extends BasicFileAttributeView and is for file systems that support the DOS attributes.
  • PosixFileAttributeView
    Extends BasicFileAttributeView and is for POSIX systems, such as Linux.
  • FileOwnerAttributeView
    For file systems that support the concept of a file owner.
  • AclFileAttributeView
    For reading or updating Access Control Lists (ACL) of a file (for example NFSv4).
  • UserDefinedFileAttributeView
    For user defined metadata (like reading MIME type).

Experiment with different views of attributes. They return null if are not applicable – e.g. getting ACL on Linux ext4.

And here’s the output of running the above example:

Pre Java 7 metadata of '/tmp':
directory: true
hidden   : false
regular  : false
symlink  : false
size     : 73728
modification time: 2016-03-23T09:17:01Z

Metadata of '/tmp':
directory: true
hidden   : false
regular  : false
is same: : true
symlink  : false
size     : 73728
owner    : root
modification time: 2016-03-23T09:17:01Z

Basic attributes of '/tmp':
directory: true
is other : false
regular  : false
symlink  : false
size     : 73728
unique id: (dev=808,ino=4718593)
access time  : 2016-03-23T08:39:42Z
creation time: 2016-03-23T09:17:01Z
modified time: 2016-03-23T09:17:01Z
owner: root
group: root

As you can see, Java 7 brought java.nio.file.Files API that allows to get even more information from underlying file system.

Was it helpful? Share!