【问题标题】:What's wrong with this file reader(loop)?这个文件阅读器(循环)有什么问题?
【发布时间】:2013-09-13 02:53:44
【问题描述】:

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

我的问题是文件不断陷入 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)

Incorrect filetype 输出是正确的(对于我正在测试的文件夹),但 IOExceptions 不正确。我知道我的代码单独适用于每个文件。

编辑 代码调用another class,它在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)

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

【问题讨论】:

  • 记录 IOException 消息,而不仅仅是“无法读取文件!”这会告诉你出了什么问题。
  • 它可能是从 checksumFinder.HexFinder() 内部抛出的,但您没有提供源代码
  • 请注意,您没有显示任何 HexFinder 代码,这很难为您提供帮助 - 而且您的 askDirectory 方法具有惊人的递归性。
  • 我的 HexFinder 代码是 here。它只是在一个文件中查找两个特定的十六进制数字并比较它们。

标签: java io ioexception


【解决方案1】:

一行

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

应该阅读

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

【讨论】:

  • 就是这样!感谢一百万!
【解决方案2】:

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

TSG_C7D4_KOI_BT_MAX_EOL.pcf**.xml**

它将文件作为 .XML 而不是 .PCF 读取。

【讨论】:

  • 我知道。这些是为了在那里,也是为了被抓住。不过谢谢:)
  • 删除你的文字,只要阅读I/O异常,它会给你更多的线索。
猜你喜欢
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多