簡體   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