属性文件无效

时间:2013-06-02 00:02:50

标签: java properties

当我运行我的程序时,我收到此错误日志:

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 = "";

    }

1 个答案:

答案 0 :(得分: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 = "";
}