-2

I have created a java code which uses diff and compares files contained in two folders.

I used diff utilities for using diff command on Windows.

I got to know about JFileChooser and Now I am trying to give a GUI to my code. Now my concern is where should i put my Java Code so that when i select two files or select two folders, diff works on them.

Below is the code for JfileChooser

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class SimpleFileChooser extends JFrame {

   public SimpleFileChooser() {
    super("File Diffrence Finder");
    setSize(350, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton1 = new JButton("Open File 1");
    JButton openButton2 = new JButton("Open File 2");
    JButton dirButton1 = new JButton("Pick Folder 1");
    JButton dirButton2 = new JButton("Pick Folder 2");
    final JLabel statusbar = 
                 new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        int option = chooser.showOpenDialog(SimpleFileChooser.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          File[] sf = chooser.getSelectedFiles();
          String filelist = "nothing";
          if (sf.length > 0) filelist = sf[0].getName();
          for (int i = 1; i < sf.length; i++) {
            filelist += ", " + sf[i].getName();
          }
          statusbar.setText("You chose " + filelist);
        }

      }
    });

    // Create a file chooser that opens up as an Open dialog
    openButton2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        int option = chooser.showOpenDialog(SimpleFileChooser.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          File[] sf = chooser.getSelectedFiles();
          String filelist = "nothing";
          if (sf.length > 0) filelist = sf[0].getName();
          for (int i = 1; i < sf.length; i++) {
            filelist += ", " + sf[i].getName();
          }
          statusbar.setText("You chose " + filelist);
        }

      }
    });


    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(SimpleFileChooser.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
                            chooser.getSelectedFile().getName():"nothing"));
        }
        else {
          statusbar.setText("You canceled.");
        }
      }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option = chooser.showOpenDialog(SimpleFileChooser.this);
        if (option == JFileChooser.APPROVE_OPTION) {
          statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
                            chooser.getSelectedFile().getName():"nothing"));
        }
        else {
          statusbar.setText("You canceled.");
        }
      }
    });

    c.add(openButton1);
    c.add(openButton2);
    c.add(dirButton1);
    c.add(dirButton2);
    c.add(statusbar);
  }

  public static void main(String args[]) {
    SimpleFileChooser sfc = new SimpleFileChooser();
    sfc.setVisible(true);
  }
}

Diff Code :

import java.io.File;
import java.util.*;

public class ListFiles
{

    public static void main(String[] args) 
    {

        String path1 = "C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing"; 

        String path2 = "C:\\Users\\hi\\Downloads\\IIT Typing\\IIT Typing"; 

        File folder1 = new File(path1);
        File folder2 = new File(path2);

        ArrayList<String> commonfiles = new ArrayList<>();

        List<File> filesList1 = Arrays.asList(folder1.listFiles());
    List<File> filesList2 = Arrays.asList(folder2.listFiles()); 


    for (File f1 : filesList1)
    {
        if(f1.isFile())
        {
        for (File f2 : filesList2) 
        {
            if(f2.isFile() && f1.getName().equals(f2.getName()))
            {
                      System.out.println(diff f1 f2);
            }
        }
    }
} 
}
}
Shubham
  • 33
  • 1
  • 10

1 Answers1

1
System.out.println(diff f1 f2);

should be

System.out.println("diff f1 f2");

Now, change the loop as :

for (File f1 : filesList1) {
    if (f1.isFile()) {
        for (File f2 : filesList2) {
            if (f2.isFile() && f1.getName().equals(f2.getName())) {
               //adding same name files here.
                commonfiles.add(f1.getName());
            }
        }
    }
}

after loop print

System.out.println("The common file names are " + commonfiles);

Updated:

Now, to check if the two files are same or not , use apache commons IO Library

Apache Commons IO

contentEquals(File file1, File file2)

Example code:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class FileCompare {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file1 = new File("c:\\test.pdf");
        File file2 = new File("c:\\test1.pdf");     
        boolean equals = FileUtils.contentEquals(file1, file2);
        System.out.println(equals);
    }

}
Makky
  • 17,117
  • 17
  • 63
  • 86
  • There is some error diff is not working... The output is nothing but 10 times 'diff f1 f2' – Shubham Nov 04 '13 at 19:50
  • That's right. It means you have 10 common files. Update your code with my loop . – Makky Nov 04 '13 at 19:54
  • What you are saying is completely wrong... Think again.... Diff in actual is not working... The output which we are getting is just because of same namef files... – Shubham Nov 04 '13 at 21:06
  • I used your code... Not working – Shubham Nov 04 '13 at 21:06
  • Could you exactly tell me what you're trying to do here ? – Makky Nov 04 '13 at 22:35
  • After Finding out same name file in two folders, I am trying to apply diff on those same name files of two different folders.. Actually I am trying to know whether these files are same or differ with each other... Its like I am comparing two different versions of a software... – Shubham Nov 05 '13 at 06:33
  • @Shubham Google for "Java library for comparing files" . – Makky Nov 05 '13 at 09:03
  • I did it.... The only option was to use diff... Nothing else... There is regex which can be of help but Noone stands near diff – Shubham Nov 05 '13 at 11:42
  • http://stackoverflow.com/questions/8001400/is-there-a-java-library-that-can-diff-two-objects See this – Shubham Nov 05 '13 at 11:44
  • Dear, could you just explain to me in plain-english what you want to achieve here , I'll help you . – Makky Nov 05 '13 at 13:09
  • Thanks for your kind gesture.... Actually what I am trying to do is first findly the files in two different folders with common names.... Secondly I am trying to find whether these files are completely same or they are different... I have achieved the first part of the problem that is to find files which have common name in two different folders... Now my second task is to find whether these files differ or not.. For that I am planning to run diff command through my Java program... I hope you understood now... – Shubham Nov 05 '13 at 15:32
  • If it helped , also thumb up the answer. – Makky Nov 06 '13 at 06:56