Open In App

Java Program to Get the Attributes of a File

Last Updated : 04 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, there are several packages and API’s to perform a lot of different functions. One such package is java.nio.file. This package contains various methods which provide support for files. A package is java.nio.file.attribute, which can be used to access the attributes of the files specified in the path object.

So basically we are using the above mentioned two packages to access the attributes of files.

  • java.nio.file: This package is used to access FileSystem class and uses the inbuilt method “getpath()” helpful to get the path object.
  • java.nio.file.attribute: This package contains a lot of classes with predefined methods to read and access the attributes of a file. Firstly getFileAttributeView() method is used to get the files attributes and then readAttributes() method is used to get the attributes of the file. Then finally several methods of the class “BasicFileAttributes” are used to display various attributes of the file.

Below shows the basic implementation of these methods to display all the attributes of the file.

Java




// Java program to get the attributes of a file
import java.nio.file.*;
import java.nio.file.attribute.*;
public class GFG {
    public static void main(String[] args) throws Exception
    {
  
        // reading the file path from the system.
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the file path");
        String s = sc.next();
  
        // setting the path
        Path path = FileSystems.getDefault().getPath(s);
  
        // setting all the file data to the attributes
        // in class file of BasicFileAttributeView.
        BasicFileAttributeView view
            = Files.getFileAttributeView(
                path, BasicFileAttributeView.class);
  
        // method to read the file attributes.
        BasicFileAttributes attribute
            = view.readAttributes();
  
        // method to check the creation time of the file.
        System.out.print("Creation Time of the file: ");
        System.out.println(attribute.creationTime());
        System.out.print(
            "Last Accessed Time of the file: ");
        System.out.println(attribute.lastAccessTime());
  
        // method to check the last
        // modified time for the file
        System.out.print(
            "Last Modified Time for the file: ");
        System.out.println(attribute.lastModifiedTime());
  
        // method to access the check whether
        // the file is a directory or not.
        System.out.println("Directory or not: "
                           + attribute.isDirectory());
  
        // method to access the size of the file in KB.
        System.out.println("Size of the file: "
                           + attribute.size());
    }
}


Output:

Some other attributes are accessed as shown below:

Java




// Java program to get the attributes of a file
import java.util.Scanner;
import java.nio.file.attribute.*;
import java.nio.file.*;
  
public
class GFG {
public
    static void main(String[] args) throws Exception
    {
  
        // reading the file path from the system.
        Scanner sc = new Scanner(System.in);
  
        System.out.println("Enter the file path");
  
        String s = sc.next();
  
        // setting the path
        Path path = FileSystems.getDefault().getPath(s);
  
        // setting all the file data to the attributes in
        // class file of BasicFileAttributeView.
        BasicFileAttributeView view
            = Files.getFileAttributeView(
                path, BasicFileAttributeView.class);
  
        // method to read the file attributes.
        BasicFileAttributes attribute
            = view.readAttributes();
  
        // check for regularity
        System.out.print("Regular File or not: ");
        System.out.println(attribute.isRegularFile());
  
        // check whether it is a symbolic file or not
        System.out.print("Symbolic File or not: ");
        System.out.println(attribute.isSymbolicLink());
  
        // type of file
        System.out.print("Other Type of File or not: ");
        System.out.println(attribute.isOther());
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads