2013-06-02 134 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 = ""; 
}