EN
Java - get metadata of the file
0
points
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");
}
}