2011-06-03 60 views
0

嗨,我需要帮助,以获得我的项目中的文件的最后修改日期我每天索引文件,我想获得修改或使用每个文件的索引只是新文件上传得到的文件修改时间

我试试这个代码

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); 
      } 
     } 
    } 
} 

,但与乐特修改时间不更改whene文件编制索引

!!!!

+0

如果您可以等待Java7,您可以使用[WatchService](http://java.sun.com/developer/technicalArticles/javase/nio/#6) – 2011-06-03 19:37:26

回答

2

你有没有考虑索引不修改文件的可能性?如果你的代码正在做索引,你需要touch一个文件来更新它的时间戳。

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

请参阅File#setLastModifield(long)

+0

我找不到文件cuz它不是我的代码我将tikaindexer作为jar文件使用 – Dilllllo 2011-06-03 20:33:29

+0

thaaaaaaaaaaaaaaaaaaaaaaaaanks其作品finaly – Dilllllo 2011-06-03 20:52:43