hi i need help to get the last modifcation date of a file in my project i index the files every day and i want to get the time of modification or using for every file to index just the new files uploaded
i try this code
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 d开发者_开发百科ate and time : " + new Date(t));
}
else{
System.out.println("File not found!");
System.exit(0);
}
}
}
}
but with that the time of laste modification dont change whene the file was indexed
!!!!
Have you considered the possibility that indexing does not modify a file? If it's your code that's doing the indexing, you need to touch a file to update its timestamp.
File f = /* whatever */;
f.setLastModified(System.currentTimeMills());
See File#setLastModifield(long)
.
精彩评论