Java - get metadata of the file

0 points
Created by:

In this article, we would like to show you how to get file metadata in Java.

Practical example

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class Example {

    public static void main(String[] args) throws IOException {

        Path filePath = Paths.get("C:\\projects\\dirask.txt");
        BasicFileAttributes fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class);

        System.out.println("isDirectory:      " + fileAttributes.isDirectory());
        System.out.println("isRegularFile:    " + fileAttributes.isRegularFile());
        System.out.println("isSymbolicLink:   " + fileAttributes.isSymbolicLink());
        System.out.println("isOther:          " + fileAttributes.isOther());

        System.out.println("creationTime:     " + fileAttributes.creationTime());
        System.out.println("lastAccessTime:   " + fileAttributes.lastAccessTime());
        System.out.println("lastModifiedTime: " + fileAttributes.lastModifiedTime());

        System.out.println("file size:        " + fileAttributes.size() + " B");

Related posts

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - file operations

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂