Compare two files using java
Compare two files using java
The compareTo method of File class helps us to create a temporary file. This method returns an integer. If the value of the integer is greater than zero, the file will be greater than the argument, if the file is less than zero, the file will be less than the argument and a value of zero means both the files are equal.
import java.io.File;
/**
* File class examples.
*
*/
public class FileExample {
public static void main(String[] args) {
// Create file object representing the source file/directory
File file = new File("d:\\temp\\test.txt");
// Create another file for comparison.
File cFile = new File("d:\\temp\\Copy of test.txtt");
// Compare two files.
int value = file.compareTo(cFile);
System.out.println("File comparison result : "+value);
}
}
Tags: File, file class examples, File examples, java.io, java.io.File
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
July 12th, 2011 at 12:12 PM
This compares the file names, not the actual files themselves. It should be noted exactly what is being compared. A file might be renamed by the OS and this wont catch it.
August 19th, 2011 at 1:20 AM
I want to compare two HTML files and then display the result in some HTML format/in some customized format. Is ther any code. pls share.
November 23rd, 2011 at 3:55 PM
This code compare file names, not file content.
January 13th, 2012 at 7:54 AM
I have got the solution
the java program should mimc the behaviour of diff command of LINUX.
http://www.simpleandeasycodes.blogspot.com/2012/01/java-compare-two-files.html