2013-06-02 131 views
-3

運行我PROGRAMM,我得到這個錯誤日誌:屬性文件不能正常工作

java.io.FileNotFoundException: config.properties (Het systeem kan het opgegeven bestand niet vinden) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:138) 
    at java.io.FileInputStream.<init>(FileInputStream.java:97) 
    at Manuals.<init>(Manuals.java:62) 
    at Manuals.main(Manuals.java:479) 

Exception in thread "main" java.lang.NullPointerException 
    at Manuals.getProjects(Manuals.java:372) 
    at Delete.<init>(Delete.java:27) 
    at Manuals.run(Manuals.java:90) 
    at Manuals.main(Manuals.java:479) 

這是行,在這裏我用我的property文件。該程序正在創建該文件,但無法讀取或編輯它。我使用了StackOverflow的一些解決方案,但沒有成功。這是我第一次調用Properties類:

public Manuals() throws IOException{ 
     // Check config file for first startup 
     configFile = new Properties(); 
     Properties configFile = new Properties(); 
     try { 
      FileInputStream file = new FileInputStream("config.properties"); 
      configFile.load(file); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(Manuals.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     curPdf = new ArrayList(); 
     addPdf = new ArrayList(); 
     allPdf = new ArrayList(); 

     this.search = ""; 

    } 
+3

哪裏是你的'config.properties'文件?你寫的代碼希望它在當前目錄中(通常是你調用java程序的目錄) – SJuan76

+0

當輸入這個文件時,文件會自動創建嗎? – user6827

+0

不,它不會。你說程序創建了這個文件,但是你的代碼沒有,你在調用'load'之前是否使用了'store'方法? – Djon

回答

1

由於堆棧跟蹤在你的臉上字面上尖叫:文件有FileInputStream訪問它之前被創建。

而不是隻記錄異常,你可以創建該文件。但是檢查它是否存在會更清晰,因爲在這種情況下,FileNotFoundException實際上是一個容器,適用於多個例外(see doc)。

我的想法是這樣的:

public Manuals() throws IOException { 
    File physicalFile = new File("config.properties"); 
    if(!physicalFile.exists()) { 
     physicalFile.createNewFile(); 
    } 

    // at this point we either confirmed that the file exists or created it 
    FileInputStream file = new FileInputStream(physicalFile); 

    Properties configFile = new Properties(); 
    configFile.load(file); 

    curPdf = new ArrayList(); 
    addPdf = new ArrayList(); 
    allPdf = new ArrayList(); 

    this.search = ""; 
}