Open In App

Java Program to Get the Basic File Attributes

Last Updated : 05 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads