-2

我创建了一个使用 diff 并比较两个文件夹中包含的文件的 java 代码。

我使用 diff 实用程序在 Windows 上使用 diff 命令。

我了解了 JFileChooser,现在我正在尝试为我的代码提供一个 GUI。现在我关心的是我应该把我的 Java 代码放在哪里,这样当我选择两个文件或选择两个文件夹时,diff 对它们起作用。

下面是 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);
  }
}

差异代码:

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);
            }
        }
    }
} 
}
}
4

1 回答 1

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

应该

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

现在,将循环更改为:

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());
            }
        }
    }
}

循环打印后

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

更新:

现在,要检查两个文件是否相同,请使用 apache commons IO Library

Apache Commons IO

contentEquals(文件文件 1,文件文件 2)

示例代码:

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);
    }

}
于 2013-11-04T19:22:11.647 回答