InputStream为null

时间:2012-04-04 09:30:49

标签: java inputstream

在我正在制作的节目中,我有

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

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

思想?

1 个答案:

答案 0 :(得分:4)

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

InputStream in = new FileInputStream(cwd + 
    file_separator + "data" + file_separator + "configuration.properties");