📜  获取文件创建时间的Java程序

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

获取文件创建时间的Java程序

使用Java.nio 包通过Java提取任何文件的创建日期和时间。要提取文件的日期和时间,请使用 BasicFileAttributes 类。 Java.nio 包帮助我们获取创建时间、上次访问时间和上次修改时间,它适用于文件和目录。

方法:

  1. 导入必要的Java库。
  2. 存储我们想要创建时间的文件的路径。
  3. 创建路径对象并将文件的路径指定到其中。
  4. 然后我们必须使用 readAttributes() 方法创建 BasicFileAttributes 类。
  5. 在 readAttributes() 方法中,我们必须传递两个参数,即路径对象和 BasicFileAttributes 类。
  6. 现在我们只需要使用属性调用 creationTime() 方法。

下面是获取文件创建时间的实现:

Java
// Java program to get the creation time of a file
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
 
public class JavafileCreationTime {
   
    public static void main() throws IOException
    {
        // storing the path of the file in the variable
        String filename
            = "C:/Users/HARDSOL/Desktop/New folder (2)";
       
        // creating the File class object
        File my_file = new File(filename);
       
        // creating the path object
        Path path = my_file.toPath();
       
        // creating BasicFileAttributes class object using
        // readAttributes method
        BasicFileAttributes file_att = Files.readAttributes(
            path, BasicFileAttributes.class);
       
        // printing the file creation time by calling
        // creationTime() method
        System.out.printf("File Creation Time %s%n ",
                          file_att.creationTime());
    }
}


Java
// Java program to get the creation time of a file
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
 
public class JavafileCreationTimee {
    public static void main() throws IOException
    {
        // storing the path of the file in the variable
        String filename
            = "C:/Users/HARDSOL/Desktop/New folder (2)";
       
        // creating the File class object
        File my_file = new File(filename);
       
        // creating the path object
        Path path = my_file.toPath();
       
        // creating BasicFileAttributes class object using
        // readAttributes method
        BasicFileAttributes file_att = Files.readAttributes(
            path, BasicFileAttributes.class);
       
        // creating simple date format object to make the
        // output more readable
        SimpleDateFormat sd
            = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
       
        System.out.print("File Creation Time: ");
       
        // converting time to milliseconds then specifying
        // the format in which we want the output
        System.out.print(
            sd.format(file_att.creationTime().toMillis()));
    }
}


输出:

下面是在 SimpleDateFormat 中获取文件创建时间的实现:

Java

// Java program to get the creation time of a file
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
 
public class JavafileCreationTimee {
    public static void main() throws IOException
    {
        // storing the path of the file in the variable
        String filename
            = "C:/Users/HARDSOL/Desktop/New folder (2)";
       
        // creating the File class object
        File my_file = new File(filename);
       
        // creating the path object
        Path path = my_file.toPath();
       
        // creating BasicFileAttributes class object using
        // readAttributes method
        BasicFileAttributes file_att = Files.readAttributes(
            path, BasicFileAttributes.class);
       
        // creating simple date format object to make the
        // output more readable
        SimpleDateFormat sd
            = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
       
        System.out.print("File Creation Time: ");
       
        // converting time to milliseconds then specifying
        // the format in which we want the output
        System.out.print(
            sd.format(file_att.creationTime().toMillis()));
    }
}

输出: