July 16, 2012

How to get the last modified image file in a folder?

This is an example program to get last modified image file from your computer


Description : The Program checks all image file in the folder specified by the user and get the properties of the image.

package imageIO;
import java.io.File;

public class Newest {

   /**
     * return the last modified file with the extension jpeg from the dirPath
     *
     * @param dirPath
     *            : directory for searching last modified jpeg file
     * @return fileName : last modified file with extension jpeg
     */
    public String lastModifiedFile(String dirPath) {
        File dir = new File(dirPath);
        File[] files = dir.listFiles();
        String fileName = files[0].getAbsolutePath();
        long fileValue = files[0].lastModified();
       
        for (int i = 0; i < files.length; i++) {
            String ext = files[i].getName().substring(
                    files[i].getName().lastIndexOf(".") + 1,
                    files[i].getName().length());
           
            if (((ext.equalsIgnoreCase("jpg") || (ext.equalsIgnoreCase("jpeg")))) & (files[i].lastModified() > fileValue)) {
               
                fileValue = files[i].lastModified();
                fileName = files[i].getAbsolutePath();
            }
        }
        return fileName;
    }

    public static void main(String[] crimea) {

        Newest newst = new Newest();
        System.out.println(newst.lastModifiedFile("H:\\GASPIPE\\MOTOX"));

    }
}


The project search for the last modified file in the folder MOTOX. The current program ( java source code) search for JPG and JPEG file. If you need to get  image files having other extensions , you need to add that condition too in the below code.

if (((ext.equalsIgnoreCase("jpg") || (ext.equalsIgnoreCase("jpeg")))) & (files[i].lastModified() > fileValue));


Copy and paste and run it...

Try the code and provide us a feedback.....

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments