测试获取文件夹目录信息

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;
import java.io.File;
import java.text.*;
 
public class fileTimeInfo
{
    public  String file_name;//文件名(带路径)
    public  Date lastmodfiyTimeDate;//文件修改时间
    public  Date CreateTimeDate;//文件创建时间
    
    public  void set_fileInfo()
    {
        Path path = Paths.get(file_name);
        BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class,LinkOption.NOFOLLOW_LINKS);
        BasicFileAttributes attr;
        try 
        {
            attr = basicview.readAttributes();
            this.lastmodfiyTimeDate=new Date(attr.lastModifiedTime().toMillis());
            this.CreateTimeDate= new Date(attr.creationTime().toMillis());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    //test main
    public static void main(String[] args) 
    {
        String path = "D:\\BaiduNetdiskDownload\\小白轻松学Spring Cloud";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(path+ "路径下的所有文件");
        System.out.println("         文件名               "+"     lastModfiedTime        "+"     creationTime     ");
        File file = new File(path);        //获取其file对象
        File[] fs = file.listFiles();
        
        for(File f:fs)
        {
            if(f.isFile())
                {
                    String f1 = f.toString().replace("\\","/");
                    fileTimeInfo fti = new fileTimeInfo();
                    fti.file_name = f1;
                    fti.set_fileInfo();
                    System.out.println(f1+"     "+df.format(fti.lastmodfiyTimeDate)+"     "+df.format(fti.CreateTimeDate));
                }
        }
    }
}

fileTimeInfo.class

Last modification:December 12th, 2020 at 09:26 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment