Comparison of two files in java
public class FileCompare {
/**
* @param args
*/
static String lineReader1 = "";
static String lineReader2 = "";
static LinkedList<String> fileList1 = new LinkedList<String>();
static LinkedList<String> fileList2 = new LinkedList<String>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JFrame f=new JFrame("File Comparator: Beta Version");//creating instance of JFrame
f.setResizable(false);
//Text Filed for CSS file
final JTextField textfield1 = new JTextField("");
textfield1.setBounds(20,50,120, 25);//x axis, y axis, width, height
// Label1
final JLabel label1 = new JLabel(" FILE1 PATH");
label1.setBounds(20,25,120, 25);
label1.setVisible(true);
f.add(label1);
//Label2
final JLabel label2 = new JLabel(" FILE2 PATH");
label2.setBounds(160,25,120, 25);
label2.setVisible(true);
f.add(label2);
//Text Filed for ODI file
final JTextField textfield2 = new JTextField("");
textfield2.setBounds(160,50,120, 25);//x axis, y axis, width, height
JButton b=new JButton("Compare");//creating instance of JButton
b.setBounds(80,100,150, 20);//x axis, y axis, width, height
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(textfield1.getText()==null || textfield1.getText().isEmpty()
&& textfield2.getText()==null||textfield2.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Provide the file names ");
}
else
{
StringBuffer sb = new StringBuffer();
try {
//READING CSS FILE
FileReader expectedFileReader = new FileReader(textfield1.getText());
BufferedReader brExpected = new BufferedReader(expectedFileReader);
while ((lineReader1 = brExpected.readLine()) != null) {
fileList1.add(lineReader1);}
//READING ODI FILE
FileReader generatedFileReader = new FileReader(textfield2.getText());
BufferedReader brGenerated = new BufferedReader(generatedFileReader);
while ((lineReader2 = brGenerated.readLine()) != null) {
fileList2.add(lineReader2);}
if(fileList1.size()==fileList2.size())
{
for ( int lineNum =1;lineNum<fileList2.size();lineNum++)
{
String sstr1 =fileList1.get(lineNum);
String sstr2 =fileList2.get(lineNum);
System.out.println("st11---"+sstr1);
System.out.println("st22-------------"+sstr2);
if(fileList1.get(lineNum).toString().equals(fileList2.get(lineNum).toString()))
{
System.out.println("Two files have same content.");
}
else if(! fileList1.get(lineNum).equals(fileList2.get(lineNum)))
{
String str1 ="FILE1 Line No:"+lineNum +"--"+fileList1.get(lineNum);
String str2= "FILE2 Line No:"+lineNum +"--"+fileList2.get(lineNum);
sb.append(str1);
sb.append(System.lineSeparator());
sb.append(str2);
sb.append(System.lineSeparator());
}
}
File f = new File("C:\\FILESS\\differnce.txt");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
new File("C:\\FILESS").mkdir();
FileWriter writer;
writer = new FileWriter("C:\\FILESS\\differnce.txt");
writer.write(sb.toString());
writer.flush();
writer.close();
JOptionPane.showMessageDialog(null, "File created at C:\\FILESS\\differnce.txt");
}
else{
System.out.println("No of lines in Both files are not equal");
JOptionPane.showMessageDialog(null, "No of lines in Both files are not equal");
}
}
catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
catch (IOException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (Exception e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
);
f.add(textfield1);//adding button in JFrame
f.add(textfield2);//adding button in JFrame
f.add(b);//adding button in JFrame
f.setSize(400,200);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Comments
Post a Comment