Languages
[Edit]
EN

Java - get metadata of the file

0 points
Created by:
Dirask Community
6810

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

Practical example

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 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

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.

❤️💻 🙂

Join