Open In App

Java Program to Get the Attributes of a File

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.



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




// 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 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:


Article Tags :