Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:49:02+00:00 2026-05-24T10:49:02+00:00

I have a folder containing image files.I need to check if any changes have

  • 0

I have a folder containing image files.I need to check if any changes have been made to the contents of this folder.For the moment ,I am just checking if the names of files in this folder have changed.

I know,that is not a good way to do it.Someone can cheat by replacing one of the images and renaming it as the replaced file..but I cannot quite figure out how to do it..should I take each file and check its modification time or some such?

Can someone suggest/illustrate an alternative?

thanks

Jim

public boolean folderContentsChanged(String folderName,String serializedCacheFileName){
    OldFileContents oldContents = readOldContents(serializedCacheFileName);
    List<String> oldFileNames =  oldContents.getListOfFileNames();
    List<String> newFileNames = createFileNamesListFromFolder(folderName);
    if(newFileNames.equals(oldFileNames)){
        return false;
    }else{
        return true;
    }

}

private OldFileContents readOldContents(String oldCacheFileName){
    FileInputStream fin = new FileInputStream(oldCacheFileName);
    ObjectInputStream oin = new ObjectInputStream(fin);
    OldFileContents oldContents =(OldFileContents) oin.readObject();
    return oldContents;

}

update:
As per trashgod’s suggestion, tried comparing hashes.. the code is given below,It takes about 55 millis for comparing two images ..

public class FileHashCompare {
    public static byte[] createByteArrayFromFile(File f) throws IOException{
        FileInputStream fis = new FileInputStream(f);
        long length = f.length();
        if (length>Integer.MAX_VALUE){
            throw new IllegalArgumentException("file too large to read");
        }
        byte[] buffer = new byte[(int)length];
        int offset = 0;
        int bytesRead = 0;
        while((offset<buffer.length) && ((bytesRead=fis.read(buffer,offset, buffer.length-offset)) >=0)  ){
             offset += bytesRead;
        }
        if (offset < buffer.length) {
            throw new IOException("Could not completely read file "+f.getName());
        }
        fis.close();
        return buffer;
    }
    public static String makeHashOfFile(File f) throws NoSuchAlgorithmException, IOException{
        String hashStr = null;
        byte[] bytes = createByteArrayFromFile(f);

        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.reset();
        md.update(bytes);
        byte[] hash = md.digest();
        hashStr = new String(hash);
        return hashStr;
    }

    public static boolean sameFile(File f1,File f2) throws NoSuchAlgorithmException, IOException{
        String hash1 =  makeHashOfFile(f1);
        String hash2 =  makeHashOfFile(f2);
        if (hash1.equals(hash2)){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {        
        long t1 = System.currentTimeMillis();
        try{
            File f1 = new File("/home/me/Pictures/painting-bob-ross-landscape-painting-1-21.jpg");
            //File f2 = new File("/home/me/Pictures/painting-bob-ross-landscape-painting-1-21 (copy).jpg");
            File f3 = new File("/home/me/Pictures/chainsaw1.jpeg");
            System.out.println("same file="+sameFile(f1,f3));
            long t2 = System.currentTimeMillis();
            System.out.println("time taken="+(t2-t1)+" millis");
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:49:03+00:00Added an answer on May 24, 2026 at 10:49 am

    Take a look at what Commons IO has: the FileAlterationMonitor might do what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my project i have a folder containing N number of files. I need
I have a project containing an image, css and js folder. I want to
I have a folder containing hundreds of TTL (TeraTermLanguage) files. Now I wanted indent
I have a folder containing images. I want to have an image slider, like
I have folder containing several files and sub-folders such as the following: -/folder -/subfolder
I have a folder containing thousand of html files. I want initially all these
Background I have a secured folder containing secret report files (in pdf format). Each
I often find myself in a situation where I have a folder containing files
I have a zip file containing a folder structure like main-folder/ subFolder1/ subFolder2/ subFolder3/
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.