File Equals
Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using Files.equal() API.
It returns TRUE if the given files contains the same bytes.
package com.bethecoder.tutorials.guava.io_tests;
import java.io.File;
import java.io.IOException;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
public class FileEqualTest {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
File file = new File ( "C:\\abcd.txt" ) ;
File file2 = new File ( "C:\\abcd.copy.txt" ) ;
//Copies source file bytes to destination file
Files.copy ( file, file2 ) ;
boolean filesEqual = Files.equal ( file, file2 ) ;
System.out.println ( "Both files are equal : " + filesEqual ) ;
//Append some text
Files.append ( "BE THE CODER" , file, Charsets.UTF_8 ) ;
filesEqual = Files.equal ( file, file2 ) ;
System.out.println ( "Both files are equal : " + filesEqual ) ;
}
}
It gives the following output,
Both files are equal : true
Both files are equal : false