This code is based on Apache common library.
If you are able to use the library, I recommend it. :-)

private boolean contentEquals(File file1, File file2) throws IOException {

    boolean file1Exists = file1.exists();

    if (file1Exists != file2.exists()) {

        return false;

    }


    if (!file1Exists) {

        // two not existing files are equal

        return true;

    }


    if (file1.isDirectory() || file2.isDirectory()) {

        // don't want to compare directory contents

        throw new IOException("Can't compare directories, only files");

    }


    if (file1.length() != file2.length()) {

        // lengths differ, cannot be equal

        return false;

    }


    if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {

        // same file

        return true;

    }


    FileInputStream fileStream1 = new FileInputStream(file1);

    FileInputStream fileStream2 = new FileInputStream(file2);


    BufferedInputStream bufferedStream1 = new BufferedInputStream(fileStream1);

    BufferedInputStream bufferedStream2 = new BufferedInputStream(fileStream2);


    int ch = bufferedStream1.read();

    while (-1 != ch) {

        int ch2 = bufferedStream2.read();

        if (ch != ch2) {

            return false;

        }

        ch = bufferedStream1.read();

    }


    int ch2 = bufferedStream2.read();


    bufferedStream1.close();

    bufferedStream2.close();


    return (ch2 == -1);

}



Posted by 배트
,