This example you’ll learn how to get file’s basic attributes. Basic file attributes are attributes that are common to many file systems and consist of mandatory and optional file attributes as defined by the BasicFileAttributes
interface.
The file’s basic attributes include file’s date time information such as the creation time, last access time, last modified time. You can also check whether the file is a directory, a regular file, a symbolic link or something else. You can also get the size of the file.
Let’s see the code snippet below:
package org.kodejava.io;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
public class FileAttributesDemo {
public static void main(String[] args) throws Exception {
String path = "D:/resources/data.txt";
Path file = Paths.get(path);
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime = " + attr.creationTime());
System.out.println("lastAccessTime = " + attr.lastAccessTime());
System.out.println("lastModifiedTime = " + attr.lastModifiedTime());
System.out.println("isDirectory = " + attr.isDirectory());
System.out.println("isOther = " + attr.isOther());
System.out.println("isRegularFile = " + attr.isRegularFile());
System.out.println("isSymbolicLink = " + attr.isSymbolicLink());
System.out.println("size = " + attr.size());
}
}
The output of the code snippet:
creationTime = 2021-05-05T17:09:09.0628061Z
lastAccessTime = 2021-05-05T17:09:09.0628061Z
lastModifiedTime = 2021-11-04T00:11:43.279Z
isDirectory = false
isOther = false
isRegularFile = true
isSymbolicLink = false
size = 0
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Hi, I am not able to access the interface
BasicFileAttributes
. Please help me in this case that how I can use the packagejava.nio
or how to usert.jar
file and how can I importrt.jar
file.Hi, to use the
BasicFileAttributes
from thejava.nio
package make sure you are using the JDK1.7
.