2011-06-03 62 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