Skip to content
Related Articles

Related Articles

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 TypeMethod Name And Description
FileTimecreationTime() – This method is used to get the creation time of the file.
FileTimelastAccessTime() – This method is used to get the last access time of the file
FileTimelastModifiedTime() – This method is used to get the last modified time of the file.
longsize() – This method is used to get the size of the file.
booleanisDirectory() – This method is used to check whether the file is a directory or not.
booleanisSymbolicLink() – This method is used to check whether the file is a symbolic link or not.
booleanisRegularFile() – This method is used to check whether the file is regular or not.
booleanisOther() – 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :