Java Program to Get the Basic File Attributes
Last Updated :
05 Feb, 2021
Basic File Attributes are the attributes that are associated with a file in a file system, these attributes are common to many file systems. In order to get the basic file attributes, we have to use the BasicFileAttributes interface. This interface is introduced in 2007 and is a part of the nio package.
The basic file attributes contain some information related to files like creation time, last access time, last modified time, size of the file(in bytes), this attributes also tell us that whether the file is regular or not, whether the file is a directory, or whether the file is a symbolic link or whether the file is something is other than a regular file, directory, or symbolic link.
Methods that are used to get the basic file attributes are:
Return Type | Method Name And Description |
---|
FileTime | creationTime() - This method is used to get the creation time of the file. |
FileTime | lastAccessTime() - This method is used to get the last access time of the file |
FileTime | lastModifiedTime() - This method is used to get the last modified time of the file. |
long | size() - This method is used to get the size of the file. |
boolean | isDirectory() - This method is used to check whether the file is a directory or not. |
boolean | isSymbolicLink() - This method is used to check whether the file is a symbolic link or not. |
boolean | isRegularFile() - This method is used to check whether the file is regular or not. |
boolean | isOther() - This method is used to check whether the file is something other than a regular file, or directory, or symbolic link. |
Below is the Java Program to get the basic file attributes:
Java
// Java Program to get the basic file attributes of the file
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;
import java.sql.Timestamp;
import java.util.Date;
public class GFG {
public static void main(String args[])
throws IOException
{
// path of the file
String path = "C:/Users/elavi/Desktop/GFG_File.txt";
// creating a object of Path class
Path file = Paths.get(path);
// creating a object of BasicFileAttributes
BasicFileAttributes attr = Files.readAttributes(
file, BasicFileAttributes.class);
System.out.println("creationTime Of File Is = "
+ attr.creationTime());
System.out.println("lastAccessTime Of File Is = "
+ attr.lastAccessTime());
System.out.println("lastModifiedTime Of File Is = "
+ attr.lastModifiedTime());
System.out.println("size Of File Is = "
+ attr.size());
System.out.println("isRegularFile Of File Is = "
+ attr.isRegularFile());
System.out.println("isDirectory Of File Is = "
+ attr.isDirectory());
System.out.println("isOther Of File Is = "
+ attr.isOther());
System.out.println("isSymbolicLink Of File Is = "
+ attr.isSymbolicLink());
}
}
Output:

Note: Above Program will run only on system IDE, it will not run on an online IDE.
Similar Reads
Java Program to Get the Creation Time of a File Use java.nio package for extracting the creation date and time of any file through java. To extract the date and time of the file use BasicFileAttributes class. java.nio package helps us to get the creation time, last access time, and last modified time, it works for both file and directory. Approac
2 min read
Java Program to Implement Attribute API Valid attribute names are case-insensitive, and they are restricted to the ASCII characters in the set of [0-9,a-z, A-Z_-], and cannot be exceeded over 70 characters in length. The attribute's value can contain any characters and will be encoded in UTF-8 when written to the output stream in any prog
6 min read
Java Directories Programs: Basic to Advanced Directories are an important part of the file system in Java. They allow you to organize your files into logical groups, and they can also be used to control access to files. In this article, we will discuss some of the Java programs that you can use to work with directories. We will cover how to cr
3 min read
Java Program to Search for a File in a Directory Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list retur
3 min read
Java Program to Create String from Contents of a File A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be lear
6 min read