开发者

get the File Modified Time

开发者 https://www.devze.com 2023-03-11 10:07 出处:网络
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 uploa

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消