HOME      BLOG       FORUMS      CONTACT       ABOUT

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

Tags: , , , ,


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.

AddThis Social Bookmark Button

4 Responses to “Compare two files using java”

  1. 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.

  2. 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.

  3. This code compare file names, not file content.

  4. 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

Leave a Reply