Home >Java >javaTutorial >Java uses the lastModified() function of the File class to obtain the last modification time of the file

Java uses the lastModified() function of the File class to obtain the last modification time of the file

PHPz
PHPzOriginal
2023-07-26 12:54:233039browse

Java uses the lastModified() function of the File class to obtain the last modification time of the file

In Java, we can use the lastModified() function of the File class to obtain the last modification time of the file. This function returns a long value representing the file modification time, in milliseconds. We can convert this value to a date object to make it easier to work with the file's last modification time.

Here is a sample code that shows how to use the lastModified() function of the File class to get the last modified time of the file and convert it into a date object:

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileLastModifiedExample {

    public static void main(String[] args) {
        // 定义文件路径
        String filePath = "C:\example.txt";
        
        // 创建File对象
        File file = new File(filePath);
        
        // 检查文件是否存在
        if(file.exists()) {
            // 获取文件的最后修改时间
            long lastModified = file.lastModified();
            
            // 将最后修改时间转换为日期对象
            Date date = new Date(lastModified);
            
            // 创建日期格式化对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            // 格式化日期对象为字符串
            String formattedDate = sdf.format(date);
            
            // 输出最后修改时间
            System.out.println("文件的最后修改时间为:" + formattedDate);
        } else {
            System.out.println("文件不存在!");
        }
    }
}

In the above code , we first define a file path filePath, and then create a File object file. Next, we ensure that the last modification time of the file we obtained is valid by determining whether the file exists.

If the file exists, we use the lastModified() function to get the last modification time of the file and convert it into a Date object. Then, we use the SimpleDateFormat class to create a date format object sdf, specifying the date format as "yyyy-MM-dd HH:mm:ss". Finally, we use the sdf.format(date) function to format the date object into a string and output it to the console.

If the file does not exist, we will output the corresponding error message.

Summary:

By using the lastModified() function of Java's File class, we can easily obtain the last modification time of the file. This is useful for applications that need to perform operations such as inspecting, comparing, and sorting files.

Note: The lastModified() function returns the timestamp of the last modification time of the file, which needs to be converted into a date object and formatted into a readable date string for easier processing.

The above is the detailed content of Java uses the lastModified() function of the File class to obtain the last modification time of the file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn