获取文件修改时间

时间:2011-06-03 19:25:35

标签: java

嗨我需要帮助来获取我项目中文件的最后修改日期我每天索引文件并且我想获得修改时间或使用每个文件来索引上传的新文件

我试试这段代码

import java.io.*;
import java.util.*;

public class GetDirectoryAndFileModifiedTime{
    public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter file or directory name in proper format to get the modification date and time : ");
        File filename = new File(in.readLine());
        if (filename.isDirectory()){
            if (filename.exists()){
                long t = filename.lastModified();
                System.out.println("Directory name : " + filename.getName());
                System.out.println("Directory modification date and time : " + new Date(t));
            }
            else{
                System.out.println("Directory not found!");
                System.exit(0);
            }
        }
        else{
            if (filename.exists()){
                long t = filename.lastModified();
                System.out.println("File name : " + filename.getName());
                System.out.println("File modification date and time : " + new Date(t));
            }
            else{
                System.out.println("File not found!");
                System.exit(0);
            }
        }
    }
}

但是,随着文件被编入索引,经过修改的时间不会改变

!!!!

1 个答案:

答案 0 :(得分:2)

您是否考虑过索引不会修改文件的可能性?如果您的代码正在进行索引编制,则需要touch一个文件来更新其时间戳。

File f = /* whatever */;
f.setLastModified(System.currentTimeMills());

请参阅File#setLastModifield(long)

相关问题