Read more : http://www.ehow.com/how_8498918_remove-title-blogger-pages.html

Sunday, 20 July 2014

Compare two text files in java

Hi,

Some times it is better to know how we can compare two text files using java.
In this post we will see how to compare two text files and writing the missed  data into another text files

Scenorio:

Need to compare database table  unique  column values before modification of sql query and after modification of sql query,After modification of the sql query few records are inserted into the table and few records are deleted from the tables  need to find what are all the records deleted from the table  and what are all the records inserted into the table.


Approaches:

1). There are several ways to compare table data : By taking table data into a temporary table and writing sql join query to achieve it.
2). Get table data into a text files and compare the text files using a java program etc etc.

In this  post we will discuss how to compare two text files using java.

In this example we  are using two text files one text file(File1) contains actual unique ids of the table, and  the other contains unique ids (create and deleted) after performing sql operations on the table.


public class IdCompare {

public static void main(String[] args) throws Exception {

List<String> firstFileList= new ArrayList<String>();
List<String> secondFileList= new ArrayList<String>();

List<String> missingRecordsInFile2= new ArrayList<String>();
List<String> missingRecordsInFile1= new ArrayList<String>();

//read file 1
Scanner firstFile=  new Scanner(new FileReader(new File("C://Users//Desktop//CompareFiles//Main.txt")));

//read file 2

Scanner secondFile=  new Scanner(new FileReader(new File("C://Users//Desktop//CompareFiles//Store6_0509.txt")));

//Write data to a file -- exits in file1 and not in file 2

FileWriter fWriteOne= new FileWriter(new File("C://Users//Desktop//CompareFiles//Output//Store6_0509.txt"));

//Write data to a file -- exits in file2 and not in file 1

FileWriter fWriteTwo= new FileWriter(new File("C://Users//Desktop//CompareFiles//Output//Main_0509.txt"));

while(firstFile.hasNext()){
firstFileList.add(firstFile.next());
}


while(secondFile.hasNext()){
secondFileList.add(secondFile.next());
}


try{

int firstCount= firstFileList.size();
int secondCount= secondFileList.size();

for(String fileOne:firstFileList){
boolean value=secondFileList.contains(fileOne);

if(!value){
missingRecordsInFile2.add(fileOne);
fWriteOne.write(fileOne);
fWriteOne.write(System.getProperty("line.separator"));
}

}

System.out.println("Total Number of records exits in file 1-->  "+firstCount+" Not exists in file 2 --> " +missingRecordsInFile2.size() );


for(String fileTwo:secondFileList){
boolean value=firstFileList.contains(fileTwo);

if(!value){
missingRecordsInFile1.add(fileTwo);
fWriteTwo.write(fileTwo);
fWriteTwo.write(System.getProperty("line.separator"));
}
}
System.out.println("Total Number of records exits in file 2-->  "+secondCount+" Not exists in file 1-->  " +missingRecordsInFile1.size() );

}finally{
fWriteOne.close();
fWriteTwo.close();
}

}
}


Hope this will help you ..