7

Java Program to Get the Attributes of a File

 3 years ago
source link: https://www.geeksforgeeks.org/java-program-to-get-the-attributes-of-a-file/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Improve Article

Java Program to Get the Attributes of a File

  • Last Updated : 04 Jan, 2021

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

gfg.png

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:

gfg.png

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK