欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 获得文件的创建时间和修改时间

程序员文章站 2022-05-24 14:37:03
...
package com.css.file;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;



public class fileTimeInfo {
	
	private static String file_name="C:/Users/CSS/Desktop/Test_Zip/出差小结_项目名称__地点_姓名.docx";
	private static Date lastmodfiyTimeDate;
	private static Date CreateTimeDate;
	
	public static void set_fileInfo(String filename){
		
		Path path = Paths.get(file_name);
		BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class,
				LinkOption.NOFOLLOW_LINKS);
		BasicFileAttributes attr;
		try {
			attr = basicview.readAttributes();
			//attr.lastModifiedTime();
			
			lastmodfiyTimeDate=new Date(attr.lastModifiedTime().toMillis());
			CreateTimeDate= new Date(attr.creationTime().toMillis());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//test main
	public static void main(String[] args) {
		set_fileInfo(file_name);
		System.out.println("lastModfiedTime	"+lastmodfiyTimeDate);
		System.out.println("creationTime	"+CreateTimeDate);
	}
}