Thursday, September 4, 2025
HomeLanguagesJavaJava Program to Get the Basic File Attributes

Java Program to Get the Basic File Attributes

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.

RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS