Java 按时修改文件

Java 按时修改文件,java,Java,您好,我需要帮助获取我的项目中文件的最后修改日期我每天对文件进行索引,我想获取每个文件的修改时间或使用时间,以便仅对上载的新文件进行索引 我试试这个代码 import java.io.*; import java.util.*; public class GetDirectoryAndFileModifiedTime{ public static void main(String[] args) throws IOException{ BufferedReader in

您好,我需要帮助获取我的项目中文件的最后修改日期我每天对文件进行索引,我想获取每个文件的修改时间或使用时间,以便仅对上载的新文件进行索引

我试试这个代码

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);
            }
        }
    }
}
但是,当文件被索引时,上次修改的时间不会改变


您是否考虑过索引不会修改文件的可能性?如果是您的代码在做索引,您需要创建一个文件来更新其时间戳

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

请参阅。

如果您可以等待Java7,您可以使用我找不到的文件,因为它不是我的代码。我使用tikaindexer作为jar文件