Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Java Code to Compare to files


Code ResourceAuthor: yuvanmytri  

Difficulty Level: Beginner

Published: 26th Jan 2011   Read: 5784 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 
Check the path for the files to be compared.
 


Sample Code
  1. import java.io.*;
  2.  
  3. public class CompareDataInFiles {
  4.  
  5.         public static void main(String[] args) throws IOException {
  6.                 // File names to Read & Write.
  7.                 String actualFile   = "C:TestJavaSampleRead.txt";
  8.                 String expectedFile = "C:TestJavaSampleWrite1.txt";
  9.                 String descptnFile  = "C:TestJavaSampleWrite2.txt";
  10.                 try{
  11.                 // Create FileReader & Writer Objects.
  12.                 FileReader actualFileReader  = new FileReader(actualFile);
  13.                 FileReader expctdFileReader  = new FileReader(expectedFile);
  14.                 FileWriter resultDesc = new FileWriter(descptnFile);
  15.                
  16.                 // Create Buffered Object.
  17.                 BufferedReader actlFileBufRdr = new BufferedReader(actualFileReader);
  18.                 BufferedReader expcFileBufRdr = new BufferedReader(expctdFileReader);
  19.                 BufferedWriter resultFileBufWrtr = new BufferedWriter(resultDesc);
  20.                
  21.                 // Get the file contents into String Variables.
  22.                 String actlFileContent = actlFileBufRdr.readLine();
  23.                 String expctdFileContent = expcFileBufRdr.readLine();
  24.                
  25.                 // Compare the Contents of the files.
  26.                 String startOfComparision = "---------START----------";
  27.                 resultFileBufWrtr.write(startOfComparision);
  28.                 resultFileBufWrtr.newLine();
  29.                 System.out.println(startOfComparision);
  30.                
  31.                 boolean isDifferent = false;
  32.                 int lineNumber = 1;
  33.                
  34.                 if (actlFileContent != null || expctdFileContent != null) {
  35.                        
  36.                         // Check whether Actual file contains data or not
  37.                         while((actlFileContent!=null)  ){
  38.                                
  39.                                 // Check whether Expected file contains data or not
  40.                                 if (((expctdFileContent )!=null)) {
  41.                                        
  42.                                         // Check whether both the files have same data in the lines
  43.                                         if (!actlFileContent.equals(expctdFileContent)) {
  44.                                                 resultFileBufWrtr.write("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains : "+expctdFileContent);
  45.                                                 resultFileBufWrtr.newLine();
  46.                                                 System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains : "+expctdFileContent);
  47.                                                 isDifferent = true;
  48.                                         }
  49.                                         lineNumber = lineNumber+1;
  50.                                         expctdFileContent= expcFileBufRdr.readLine();
  51.                                 }
  52.                                 else{
  53.                                         resultFileBufWrtr.write("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  54.                                         resultFileBufWrtr.newLine();
  55.                                         System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  56.                                         isDifferent = true;
  57.                                         lineNumber = lineNumber+1;
  58.                                 }
  59.                                 actlFileContent=actlFileBufRdr.readLine();
  60.                         }
  61.                        
  62.                         // Check for the condition : if Actual File has Data & Expected File doesn't contain data.
  63.                         while ((expctdFileContent!=null)&&(actlFileContent==null)) {
  64.                                 resultFileBufWrtr.write("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  65.                                 resultFileBufWrtr.newLine();
  66.                                 System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  67.                                 isDifferent = true;
  68.                                 lineNumber = lineNumber+1;
  69.                                 expctdFileContent= expcFileBufRdr.readLine();
  70.                         }
  71.                 }
  72.                 else{
  73.                         // Mention that both the files don't have Data.
  74.                         resultFileBufWrtr.write("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  75.                         resultFileBufWrtr.newLine();
  76.                         System.out.println("Difference in Line "+lineNumber+" :- Actual File contains :"+actlFileContent+", Expected File Contains - "+expctdFileContent);
  77.                         isDifferent = true;
  78.                 }
  79.                
  80.                 // Check is there any difference or not.
  81.                 String endOfComparision = "-----------END----------";
  82.                 if (isDifferent) {
  83.                         resultFileBufWrtr.append(endOfComparision);
  84.                         System.out.println(endOfComparision);
  85.                 }
  86.                 else{
  87.                         resultFileBufWrtr.append("No difference Found");
  88.                         resultFileBufWrtr.newLine();
  89.                         resultFileBufWrtr.append(endOfComparision);
  90.                        
  91.                         System.out.println("No difference Found");
  92.                         System.out.println(endOfComparision);
  93.                 }
  94.                
  95.                 //Close the streams.
  96.                 actualFileReader.close();
  97.                 expctdFileReader.close();
  98.                 resultFileBufWrtr.close();
  99.                 actlFileBufRdr.close();
  100.                 expcFileBufRdr.close();
  101.           }
  102.           catch( FileNotFoundException e )
  103.         {
  104.                         e.printStackTrace();
  105.         }              
  106.         }
  107.  
  108. }
  109.  
Copyright GeekInterview.com


Next Article: System Info using Java


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora140724
2. iamdvr102875
3. venki_madesh35424
4. Raju20171
5. parmod kumar duhan16408
6. Kiran.jakkaraju15125
7. chowsys7787
8. ashish.cns6884
9. Venkateswara Rao6774
10. venkat_kiran5895

Active Coders

Refined Tags