繁体   English   中英

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

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

我编写了一个程序,提示用户选择特定目录。 一旦完成,程序应选择该文件夹中的每个文件,然后对这些单独的文件执行其他代码(与该问题无关)。

我的问题是文件不断陷入try / catch IO异常中,我无法弄清原因。

以下是我的文件选择器代码和输出。

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

输出:

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)

不正确的文件类型输出正确(对于我正在测试的文件夹),但IOExceptions不正确。 我知道我的代码可以分别处理每个文件。

编辑该代码调用另一个在try / catch中使用缓冲读取器的 当此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)

谁能看到我哪里出问题了? 一如既往地感谢任何帮助。

线

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

应该读

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

查找文件名和扩展名问题。 如您所见,这是FileName和扩展名的问题

TSG_C7D4_KOI_BT_MAX_EOL.pcf**.xml**

它以.XML而不是.PCF格式读取文件。

暂无
暂无

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

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