我编写了一个程序,提示用户选择一个特定的目录。完成后,程序应选择该文件夹中的每个文件,然后对这些单个文件执行其他代码(与此问题无关)。
我的问题是文件不断被 try/catch IO Exception 捕获,我不知道为什么。
下面是我的文件选择器代码和输出。
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)
谁能看到我哪里出错了?一如既往地感谢任何帮助。