This program will compare any two text files (which contains text only) and give the output as whether the contents of two files are same or not.
import java.io.*; import java.util.*; import java.io.IOException; public class CompareTextFiles{ public static void main(String[] arg){ CompareTextFiles1 ctf = new CompareTextFiles1(); ctf.compareFiles(); //calling method from the CompareTextFiles1 class to compare the two text files. } } class CompareTextFiles1 { public void compareFiles(){ String File1 = null; String File2 = null; try { File1 = getTextFile("Give the path for the first file containing text only"); // loading the first text file to compare File2 = getTextFile("Give the path for the second file containing text only"); // loading the second text file to compare File1 = File1.replace(' ', ' '); File1 = File1.replace(' ', ' '); File1 = File1.replace(' ', ' '); File1 = File1.replace(' ', ' '); File2 = File2.replace(' ', ' '); File2 = File2.replace(' ', ' '); File2 = File2.replace(' ', ' '); File2 = File2.replace(' ', ' '); if (File1.equals(File2)) System.out.println("The contents of two files are same."); else System.out.println("The contents of two files are different."); }catch (IOException e) { System.err.println ("Getting IOException"); } } /* Method reading the contents of a file and returning it as a string to the calling program */ public String getTextFile(String fileName) throws IOException { StringBuffer buffer = null; FileInputStream fin = new FileInputStream (fileName); try { buffer = new StringBuffer(); String next; while ((next = new DataInputStream(fin).readLine()) != null) { buffer.append(next); buffer.append(" "); } } catch (IOException ioe) { throw ioe; } finally { if (fin != null) fin.close(); } return buffer.toString(); } }