开发者

how to check in java, if contents of an imagefolder has changed

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 fil开发者_如何学JAVAe "+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();
        }

    }

}


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


Basically that's the correct way for Java6, but you have to change List<String> to List<File>

But that I thing that you have problem with compare Files as File, Files and String forms too, everything is on this forum.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜