4

在我正在研究的一个程序中,我有

String cwd;
String file_separator;

public ConfigLoader()
{
    cwd = get_cwd();
    file_separator = get_file_separator();

    try
    {
        Properties c = new Properties();

        InputStream in = this.getClass().getClassLoader().getResourceAsStream(cwd + 
            file_separator + "data" + file_separator + "configuration.properties");

        c.load(in);
    }
    except (Exception e) { e.printStackTrace(); }
}

public String get_file_separator()
{
    File f = new File("");
    return f.separator;
}

public String get_cwd()
{
    File cwd = new File("");
    return cwd.getAbsolutePath();
}

但是,由于某种原因,c.load(in);会导致NullPointerException. 例外来自in == NULL真实。我不知道为什么,因为

System.out.println(cwd + file_separator + "data" + file_separator +
    "configuration.properties");

印刷

/users/labnet/st10/jjb127/workspace/Brewer-Client/data/configuration.properties

这是我要使用的文件的位置。

想法?

4

1 回答 1

6

getResourceAsStream旨在搜索类路径上的文件,而不是访问本地文件系统。您将不得不使用FileInputStream这种情况。

InputStream in = new FileInputStream(cwd + 
    file_separator + "data" + file_separator + "configuration.properties");
于 2012-04-04T09:32:12.533 回答