Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import changeprone.*; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; import org.apache.commons.io.FileUtils; /** * * @author aryan_000 */ public class Compare extends javax.swing.JFrame { /** * Creates new form Compare */ ArrayList<Files> filenames1 = new ArrayList<>(); ArrayList<Files> filenames2 = new ArrayList<>(); public void listofFiles(File folder) { for (File testfile : folder.listFiles()) { if (testfile.isDirectory()) { listofFiles(testfile); } else { // System.out.println(testfile); // System.out.println(testfile.getName()); // System.out.println("Size of the file is : " + testfile.length() + "Bytes"); String ext = null; String temp = testfile.getPath(); int i = temp.lastIndexOf('.'); if (i > 0) ext = temp.substring(i + 1); if (ext.equals("java")) { // filenames1.add(new Files(testfile.getName(), testfile.length(), testfile, testfile.getPath())); } } } } // end of listfo files function public void listofFiles1(File folder) { for (File testfile : folder.listFiles()) { if (testfile.isDirectory()) { listofFiles1(testfile); } else { // System.out.println(testfile); // System.out.println(testfile.getName()); // System.out.println("Size of the file is : " + testfile.length() + "Bytes"); String ext = null; String temp = testfile.getPath(); int i = temp.lastIndexOf('.'); if (i > 0) ext = temp.substring(i + 1); if (ext.equals("java")) { // filenames2.add(new Files(testfile.getName(), testfile.length(), testfile, testfile.getPath())); } } } } // end of listfo files function public void sortByName() { Collections.sort(filenames1, (Files o1, Files o2) -> o1.Filename.compareTo(o2.Filename)); } public void sortByName1() { Collections.sort(filenames2, (Files o1, Files o2) -> o1.Filename.compareTo(o2.Filename)); } public Compare() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); FileDetails = new javax.swing.JTable(); jScrollPane2 = new javax.swing.JScrollPane(); FileDetails1 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Select Folder"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); FileDetails.setAutoCreateRowSorter(true); FileDetails.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "S.No", "File Names", "File Size", "File Location", "Status" }) { boolean[] canEdit = new boolean[] { false, false, false, false, true }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(FileDetails); FileDetails1.setAutoCreateRowSorter(true); FileDetails1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "S.No", "File Names", "File Size", "File Location", "Status" }) { boolean[] canEdit = new boolean[] { false, false, false, false, true }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane2.setViewportView(FileDetails1); jButton2.setText("Select Folder 2"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Compare"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 31, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(72, 72, 72).addComponent(jButton1) .addGap(228, 228, 228).addComponent(jButton2)) .addGroup(layout.createSequentialGroup().addGap(252, 252, 252).addComponent(jButton3))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(17, 17, 17).addComponent(jButton1)) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jButton2))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE) .addComponent(jButton3).addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String userDir = System.getProperty("user.home"); JFileChooser folder = new JFileChooser(userDir + "/Desktop"); folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); folder.setFileSelectionMode(JFileChooser.FILES_ONLY); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Excel Files (*.xls)", "xls"); folder.setFileFilter(xmlfilter); int returnvalue = folder.showSaveDialog(this); File myfolder = null; if (returnvalue == JFileChooser.APPROVE_OPTION) { myfolder = folder.getSelectedFile(); // System.out.println(myfolder); } if (myfolder != null) { JOptionPane.showMessageDialog(null, "The current choosen file directory is : " + myfolder); } listofFiles(myfolder); sortByName(); DefaultTableModel model = (DefaultTableModel) FileDetails.getModel(); int count = 1; for (Files filename : filenames1) { String size = Long.toString(filename.Filesize) + "Bytes"; model.addRow(new Object[] { count++, filename.Filename, size, filename.FileLocation }); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: String userDir = System.getProperty("user.home"); JFileChooser folder = new JFileChooser(userDir + "/Desktop"); folder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); folder.setFileSelectionMode(JFileChooser.FILES_ONLY); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Excel Files (*.xls)", "xls"); folder.setFileFilter(xmlfilter); int returnvalue = folder.showSaveDialog(this); File myfolder = null; if (returnvalue == JFileChooser.APPROVE_OPTION) { myfolder = folder.getSelectedFile(); // System.out.println(myfolder); } if (myfolder != null) { JOptionPane.showMessageDialog(null, "The current choosen file directory is : " + myfolder); listofFiles1(myfolder); sortByName1(); } DefaultTableModel model = (DefaultTableModel) FileDetails1.getModel(); int count = 1; System.out.println(filenames2.size()); for (Files filename : filenames2) { String size = Long.toString(filename.Filesize) + "Bytes"; model.addRow(new Object[] { count++, filename.Filename, size, filename.FileLocation }); } }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: DefaultTableModel model1 = (DefaultTableModel) FileDetails.getModel(); DefaultTableModel model2 = (DefaultTableModel) FileDetails1.getModel(); int count = 1; Boolean flag = false; for (Files filename : filenames1) { // String size = Long.toString(filename.Filesize) + "Bytes"; // model.addRow(new Object[]{count++,filename.Filename, size , filename.FileLocation}); boolean compare1 = false; for (Files fname : filenames2) { if (filename.Filename.equals(fname.Filename)) { File file1 = new File(filename.FileLocation); File file2 = new File(fname.FileLocation); flag = true; try { compare1 = FileUtils.contentEquals(file1, file2); } catch (IOException ex) { Logger.getLogger(Compare.class.getName()).log(Level.SEVERE, null, ex); } String size = Long.toString(filename.Filesize) + "Bytes"; String status; if (flag) { if (compare1) status = "same"; else status = " changed"; } else status = "deleted"; // model1.addColumn(count, new Object[] { status} ); model1.addRow(new Object[] { count++, filename.Filename, size, filename.FileLocation, status }); } } } }//GEN-LAST:event_jButton3ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Compare.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Compare.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Compare.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Compare.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Compare().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTable FileDetails; private javax.swing.JTable FileDetails1; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; // End of variables declaration//GEN-END:variables }