Làm thế nào để biết thời điểm cuối cùng tập tin được chỉnh sửa? (How to get the file last modified date in Java?)






Chắc bạn biết trong Properties của một tập tin có thông tin ngày giờ cuối cùng mà tập tin được chỉnh sửa. Bài viết này sẽ hướng dẫn cách lấy thời gian này trên tập tin trong ngôn ngữ lập trình Java.






Ta sẽ cần một tâp tin để làm ví dụ cho bài viết này.



Và thông tin ta cần lấy là:

Modified: Saturday, January 18, 2014, 6:50:06 PM

Đoạn script dưới đây sẽ giúp ta thực hiện yêu cầu trên.

/**
 * @(#)JavaGetModifiedDateFile.java
 *
 * JavaGetModifiedDateFile application
 *
 * @author developer.bnson@gmail.com
 * @version 1.00 2014/3/15
 */

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;

public class JavaGetModifiedDateFile {
   
    public static void main(String[] args) {
       
        File file =new File("D:\\Z-Test\\VNLIVES_NET\\VNLIVES.NET.jpg");
       
        try {
           
            BasicFileAttributes bfa = Files.readAttributes(file.toPath(), BasicFileAttributes.class);   
            System.out.println("Modified Date File: " + bfa.lastModifiedTime());
               
        } catch (Exception ex) {
           
        }       
       

    }
}

Kết quả sau khi chạy chương trình sẽ là:

Modified Date File: 2014-01-18T06:50:06.984375Z























No comments:

Post a Comment