如果图像文件夹的内容发生了变化,如何检查java

时间:2011-08-07 05:25:20

标签: java file directory

我有一个包含图像文件的文件夹。我需要检查是否对此文件夹的内容进行了任何更改。目前,我只是检查此文件夹中的文件名是否已更改。

我知道,这不是一个好方法。有人可以通过替换其中一个图像并将其重命名为替换文件而作弊。但我无法弄清楚如何做到这一点。我应该采取各自的方法。文件,并检查其修改时间或一些这样的?

有人可以建议/说明替代方案吗?

感谢

吉姆

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;

}

更新: 根据trashgod的建议,尝试比较哈希..下面给出了代码,比较两个图像需要大约55毫米..

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();
        }

    }

}

2 个答案:

答案 0 :(得分:2)

看看Commons IO有什么:FileAlterationMonitor可能会做你想做的事。

答案 1 :(得分:1)

基本上这是Java6的正确方法,但您必须将List<String>更改为List<File>

但是我认为比较文件有FileFilesString形式存在问题,一切都在这个论坛上。

相关问题