【问题标题】:Change fileChooser to multiple-fileChooser and iterate through those files将 fileChooser 更改为 multiple-fileChooser 并遍历这些文件
【发布时间】:2013-09-07 21:50:31
【问题描述】:

我已经编写了代码来打开和读取(特定文件类型的)单个文件,但现在需要做同样的事情,只需要一个包含这些文件的文件夹。我找到了this question,但我不知道如何根据需要更改它。

这是我当前的代码。

    JFileChooser inFileName = new JFileChooser(new 
    File("C:\\Documents and Settings\\lucey01\\Desktop\\Projects\\C0048817\\KOI\\C0048817_PCF_Front"));       
    \\This is the default folder

    FileNameExtensionFilter filter = new FileNameExtensionFilter("PCF & TXT Files", "pcf", "txt");
    inFileName.setFileFilter(filter);
    
    Component parent = null;
    
    do {
      returnVal1 = inFileName.showOpenDialog(parent);
      if (returnVal1 == JFileChooser.CANCEL_OPTION) {
          returnVal2 = JOptionPane.showConfirmDialog(null, "Select YES to cancel. Select NO to restart",
          "Are you sure?", JOptionPane.YES_NO_OPTION);
          if (returnVal2 == JOptionPane.YES_OPTION) {
            System.exit(returnVal2);
            }else{
             checksumGUI.this.askDirectory();
           }
      }
   } while (returnVal1 == JOptionPane.CANCEL_OPTION);
     cf.HexFinder(inFileName,null,null,null);

编辑 所以我的问题是:我可以向此代码添加/更改任何(小)东西以使其适用于文件夹中特定类型的多个文件吗?

非常感谢任何帮助,谢谢。

【问题讨论】:

  • “我不知道如何根据需要更改它。” ..你有问题吗?请注意,如果问题是“如何做到这一点?”它将成为关闭的候选者,因此请尝试将问题具体化。
  • 我明白了,因为我使用了 JFileChooser,它需要的不仅仅是一个 For 循环来让我的代码读取多个文件。所以我的问题是,如何实现文件阅读器来读取文件夹中的所有文件?(只有一次 GUI 迭代)
  • 我没有关注你。你知道setMultiSelectionEnabled(boolean)吗?如果启用,只需显示一次文件选择器,用户就可以在目录中选择任意数量的文件。
  • 如何实现?
  • 我会用 Java 代码实现它。你为什么不试试呢?如果您没有成功,请发布您的最佳尝试SSCCE,并再次提出与该尝试相关的明确问题。

标签: java file io directory


【解决方案1】:

终于有答案了。我的问题是我需要把它放到一个单独的方法中。

这里发生的是一个文件夹被选择而不是一个单独的文件。代码遍历该文件夹中的每个文件。以 .pcf 结尾的文件是唯一调用下一个类来读取该文件的文件

这就是缺少的东西;

...
//Insert right after the JFilechoser is created
inFileName2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
...
public void multiFile(JFileChooser inFileName) throws FileNotFoundException, IOException {

    checksumFinder cf = new checksumFinder(this);//Calls the class to read the file

     File[] listAllFiles = inFileName.getSelectedFile().listFiles

    for (int i = 0; i < listAllFiles.length; i++) {

        File currentFile = listAllFiles[i];

        if (currentFile.isFile() && currentFile.getName().endsWith(".pcf")) {
            cf.hexFinder(currentFile, null, null, null);
        } 
     }

【讨论】:

    【解决方案2】:

    好的,这是我的 SSCCE。我发现的一些代码来自here,非常有用。 这段代码将编译,似乎没有问题。但是经过一点调试我仍然不知道为什么它不起作用

    package robertskostalproject;
    
    import java.awt.Component;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import javax.swing.*;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class checksumGUI {
    
     private checksumFinder cf = new checksumFinder();
     private static int returnVal1;
     private static int returnVal2;
    
    public void askDirectory() throws FileNotFoundException, UnsupportedEncodingException, IOException {
    
        JFileChooser inFileName = new JFileChooser(new File("C:\\Documents and Settings\\lucey01\\Desktop\\Projects\\C0048817\\KOI"));
        inFileName.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    
        Component parent = null;
    
        do {
    
         returnVal1 = inFileName.showOpenDialog(parent);
         if (returnVal1 == JFileChooser.CANCEL_OPTION) {
            returnVal2 = JOptionPane.showConfirmDialog(null, "Select YES to cancel. Select NO to restart",
            "Are you sure?", JOptionPane.YES_NO_OPTION);
              if (returnVal2 == JOptionPane.YES_OPTION) {
                 System.exit(returnVal2);
              } else {
                checksumGUI.this.askDirectory();
             }
          }
    
        } while (returnVal1 == JOptionPane.CANCEL_OPTION);
    
      File folder = inFileName.getSelectedFile();
      File[] listOfFiles = folder.listFiles();
    
     for (int i = 0; i < listOfFiles.length; i++) {
       File file = listOfFiles[i];
       if (file.isFile() && file.getName().endsWith(".txt") || file.getName().endsWith(".pcf")) {
         cf.HexFinder(inFileName, null, null, null);
       }else {
        System.out.println("File was not of correct type");
       }
      }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2017-07-19
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多