📜  获取文件属性的Java程序

📅  最后修改于: 2022-05-13 01:54:43.039000             🧑  作者: Mango

获取文件属性的Java程序

在Java中,有几个包和 API 来执行许多不同的功能。一个这样的包是Java.nio.file。这个包包含各种为文件提供支持的方法。一个包是Java.nio.file.attribute,它可以用来访问路径对象中指定的文件的属性。

所以基本上我们使用上面提到的两个包来访问文件的属性。

  • Java.nio.file:该包用于访问 FileSystem 类,并使用内置方法“getpath()”来帮助获取路径对象。
  • Java.nio.file.attribute:这个包包含了很多带有预定义方法的类来读取和访问文件的属性。首先使用 getFileAttributeView() 方法获取文件属性,然后使用 readAttributes() 方法获取文件属性。然后最后使用“BasicFileAttributes”类的几个方法来显示文件的各种属性。

下面显示了这些方法的基本实现,以显示文件的所有属性。

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());
    }
}


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());
    }
}


输出:

其他一些属性的访问方式如下所示:

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());
    }
}

输出: