Friday, 11 March 2016

Java code to get last modified date of files from directory

//Java code to get last modified date of files from directory

import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.activation.MimetypesFileTypeMap;

public class FilterFilesFromFolder {
    
   
    public static <lang> void main(String a[]){
        File file = new File("E:/sample/");
     
        //filtering txt from folder
        FilenameFilter textFilter = new FilenameFilter()
        {
            public boolean accept(File dir, String name) {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".txt")) {
                    return true;
                } else {
                    return false;
                }
            }
        };
       
        File[] files = file.listFiles(textFilter);
       
        long min = 0;
        String mimeType;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date d = new Date();
        System.out.println("System Date :"+ sdf.format(d));
        for(File f: files){
            System.out.println("File Name ="+f.getName());
            System.out.println("Date Last Madified ="+ sdf.format(f.lastModified()));
            mimeType = new MimetypesFileTypeMap().getContentType(f);
            System.out.println("MIME Type ="+ new MimetypesFileTypeMap().getContentType(f));
           
            //getting last one hour modified files
            try
            {
                 long cd = d.getTime();
                 long lmd = f.lastModified();
                 long diff= cd-lmd;
                 System.out.println("Diff ="+diff);
                min = diff/(60 * 1000);
                 System.out.println("Diff in Min = "+min);
            }
            catch(Exception e)
            {
                System.out.println("Error in Date Diffrence = " + e);
            }
            if (min <= 60)
            {
            System.out.println("send mail");   
            }
        }
    }
}

No comments:

Post a Comment