简体   繁体   中英

What's wrong with this file reader(loop)?

I have written a program that prompts the user to select a specific directory. Once that is done the program should select each file in that folder and then carry out other code (not relevant to this question) on those individual files.

My problem is that the files keep getting caught in the try/catch IO Exception and I cannot figure out why.

Below is my file chooser code and output.

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\\C0048817_PCF_Front"));
    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 folderFile = inFileName.getSelectedFile();
    File[] listOfFiles = folderFile.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (file.isFile() && file.getName().endsWith(".pcf")) {
         cf.HexFinder(folderFile, null, null, null);
        }else {
          System.out.println("Incorrect filetype:\n" + file.getName() + "\n");
        }
    }
}
}

output:

run:
IO Exception: Could not read file!

Incorrect filetype:
TSG_C7D4_KOI_BT_MAX_EOL.pcf.xml

IO Exception: Could not read file!

Incorrect filetype:
TSG_C7D4_KOI_BT_MAX_PLUS_EOL.pcf.xml

IO Exception: Could not read file!

BUILD SUCCESSFUL (total time: 2 seconds)

The Incorrect filetype outputs are correct (for the folder I was testing) but the IOExceptions aren't. I know my code works on each file individually.

EDIT The code calls another class that uses a Buffered Reader inside the try/catch. When this BufferedReader is outside the try/catch I get the following error:

run:
Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\lucey01\Desktop\Projects\C0048817\KOI\C0048817_PCF_Front (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at robertskostalproject.checksumFinder.HexFinder(checksumFinder.java:24)
at robertskostalproject.checksumGUI.askDirectory(checksumGUI.java:47)
at robertskostalproject.RobertsKostalProject.main(RobertsKostalProject.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Can anyone can see where I've gone wrong? Any help is much appreciated as always.

the line

cf.HexFinder(folderFile, null, null, null);

should read

cf.HexFinder(file, null, null, null);

look for file name and extension issue. it is a problem with FileName and extension as you see

TSG_C7D4_KOI_BT_MAX_EOL.pcf**.xml**

it is reading the file as .XML not .PCF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM