使用Jframe时在哪里放java代码?

时间:2013-11-04 18:37:45

标签: java swing jframe diff

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

我使用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);
            }
        }
    }
} 
}
}

1 个答案:

答案 0 :(得分: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(File file1, File file2)

示例代码:

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

}