Java NIO Read File Metadata / Attributes Example

How to read File Metadata in Java NIO?


In this example, we will explain how to use Java NIO classes, to read the following attributes / metadata of any file available in the file system. More specifically, we will discuss how to read the following metadata information against any file:

  • Last Modified Time
  • Last Access Time
  • Creation Time
  • Size of the File in bytes
  • Is a regular File?
  • Is the file a directory?
  • Is the file a symbolic link?
  • Is the file other than a regular file, directory or symbolic link?
We will not be discussing how to update these file attributes. This tutorial focusses only reading these file attributes using Java. You will be surprised to note, how easy it is to use NIO to access file information.

Use - BasicFileAttributeView to read metadata


All the metadata information we are trying to read must be supported by all operating systems as de facto standard, and is exposed in a view delivered in Java NIO.2.  The view name for this is "basic". We will shortly provide a Java program that will use this view to access file properties.

readAttributes method in java.nio.file.Files


The method readAttributes defined in java.nio.file.Files class will help to read the basic file attribute information in Java. This method returns the attributes of the file as bulk information of type, BasicFileAttributes. Without much theory, the step by step program to read file attributes is provided below:

Access file to Read attributes:


The first step is to use Path object to access the file for which we want to read metadata. The Java code for this is provided below:
        /* Step  -1: Access the file in Path object */
        Path path = Paths.get("C:", "source.log");

As you can see above, we are trying to read the attributes for the file source.log present in my c: drive.

Use readAttributes method to read basic attributes


In this step, we get the attributes of the file into BasicFileAttributes  method by using readAttributes method. As an example;
        /* use readAttributes to read file attributes */
        BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);

Print metadata of  a File


Once we have access to attributes object, we can print all the file attributes. Some simple print statements that fetches the metadata is provided below:
        /* Print File Attributes */
        System.out.println("Size of the File in Bytes: " + attributes.size());
        System.out.println("File Creation TimeStamp: " + attributes.creationTime());
        System.out.println("File Last Access TimeStamp: " + attributes.lastAccessTime());
        System.out.println("File Last Modified TimeStamp: " + attributes.lastModifiedTime());
        System.out.println("Is the File a Directory? " + attributes.isDirectory());
        System.out.println("Is the File a regular File? " + attributes.isRegularFile());
        System.out.println("Is the File a symbolic link? " + attributes.isSymbolicLink());
        System.out.println("Is the File other than regular, Directory and Symbolic link? " + attributes.isOther());             

Complete Java Program


Putting it all together, the complete Java program to read file attributes using NIO is provided below:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class FileAttributes {
    public static void main(String[] args) throws Exception {
        /* Step  -1: Access the file in Path object */
        Path path = Paths.get("C:", "source.log");
        /* use readAttributes to read file attributes */
        BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
        /* Print File Attributes */
        System.out.println("Size of the File in Bytes: " + attributes.size());
        System.out.println("File Creation TimeStamp: " + attributes.creationTime());
        System.out.println("File Last Access TimeStamp: " + attributes.lastAccessTime());
        System.out.println("File Last Modified TimeStamp: " + attributes.lastModifiedTime());
        System.out.println("Is the File a Directory? " + attributes.isDirectory());
        System.out.println("Is the File a regular File? " + attributes.isRegularFile());
        System.out.println("Is the File a symbolic link? " + attributes.isSymbolicLink());
        System.out.println("Is the File other than regular, Directory and Symbolic link? " + attributes.isOther());             
    }
}

You can then use these attributes in your code for further manipulation.

Example output:


An example output for the code above is provided below:
Size of the File in Bytes: 4263
File Creation TimeStamp: 2012-07-25T00:30:41.694249Z
File Last Access TimeStamp: 2012-12-19T06:57:21.603217Z
File Last Modified TimeStamp: 2012-07-25T03:33:58.603823Z
Is the File a Directory? false
Is the File a regular File? true
Is the File a symbolic link? false
Is the File other than regular, Directory and Symbolic link? false

That completes the beginners tutorial to access file metadata information using BasicFileAttributes class in Java NIO. See you in a different tutorial next time.

No comments:

Post a Comment