0

I've written code to open and read individual files (of specific filetypes) but now need to do the same, only with a folder full of these files. I've found this question but I have no idea how to change it around for what I need.

Here's my current code.

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

EDIT So my question is: Is there any (small) thing I can add/change to this code to get it to work on multiple files of specific types in a folder?

Any help is much appreciated, thanks.

Community
  • 1
  • 1
Robert English
  • 223
  • 1
  • 13
  • *"I have no idea how to change it around for what I need."* ..do you have a question? Be warned that if the question is 'How to do this?' it will be a candidate for closure, so try to make the question specific. – Andrew Thompson Sep 04 '13 at 12:05
  • I see that because I've used _JFileChooser_ it will require more than just a _For_ loop to get my code to read multiple files. So my question is, _How can I implement a file reader to read all files in a folder?_ (with only one iteration of the GUI) – Robert English Sep 04 '13 at 12:15
  • 1
    I'm not following you. Are you aware of [`setMultiSelectionEnabled(boolean)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setMultiSelectionEnabled%28boolean%29)? If that is enabled, it only takes one showing of the file chooser for the user to select as many files in a directory as they want. – Andrew Thompson Sep 04 '13 at 12:19
  • How could that be implemented? – Robert English Sep 04 '13 at 13:04
  • I'd implement it in Java code. Why don't you try that? If you don't succeed, post an [SSCCE](http://sscce.org/) of your best attempt, again with an explicit question related to the attempt. – Andrew Thompson Sep 04 '13 at 13:51
  • Are you trolling? You are making this much harder than it needs to be. I've given my question, edited it and given my code and a related question to clarify what I'm looking for. I've **tried** to do this and failed, so I came here for help. Judging from your Rep I'm sure you could answer this very easily. If you want to help please don't play around. – Robert English Sep 04 '13 at 14:03
  • *"If you want to help please don't play around."* If you want an answer, follow my advice. If you want someone to code this for you - hire them. That is not what SO is for. – Andrew Thompson Sep 04 '13 at 14:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36796/discussion-between-robert-english-and-andrew-thompson) – Robert English Sep 04 '13 at 14:12
  • I've just tried using the _setMultiSelectionEnabled(boolean)_. It let me select multiple files but the code only read one. Why is that? – Robert English Sep 04 '13 at 14:48

2 Answers2

0

Ok, here's my SSCCE. Some of the code I found was from here, very useful. This code will compile and there seems to be no trouble with it. But after a little debugging I still don't know why it won't work

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");
   }
  }
 }
}
Community
  • 1
  • 1
Robert English
  • 223
  • 1
  • 13
0

Finally got an answer for this. My problem was that I needed to put it onto a seperate method.

What happens here is that a Folder is chosen instead of an indivual file. The code iterates through each file inside that folder. The files that end with .pcf are the only ones called to the next class to read that file

Here's what was missing;

...
//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);
        } 
     }
Robert English
  • 223
  • 1
  • 13