简体   繁体   English

这个文件阅读器(循环)怎么了?

[英]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. 我的问题是文件不断陷入try / catch IO异常中,我无法弄清原因。

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. 不正确的文件类型输出正确(对于我正在测试的文件夹),但IOExceptions不正确。 I know my code works on each file individually. 我知道我的代码可以分别处理每个文件。

EDIT The code calls another class that uses a Buffered Reader inside the try/catch. 编辑该代码调用另一个在try / catch中使用缓冲读取器的 When this BufferedReader is outside the try/catch I get the following error: 当此BufferedReader在try / catch之外时,出现以下错误:

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 如您所见,这是FileName和扩展名的问题

TSG_C7D4_KOI_BT_MAX_EOL.pcf**.xml**

it is reading the file as .XML not .PCF. 它以.XML而不是.PCF格式读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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