InputStream为null

4 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();
}
Run Code Online (Sandbox Code Playgroud)

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

System.out.println(cwd + file_separator + "data" + file_separator +
    "configuration.properties");
Run Code Online (Sandbox Code Playgroud)

版画

/users/labnet/st10/jjb127/workspace/Brewer-Client/data/configuration.properties
Run Code Online (Sandbox Code Playgroud)

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

有什么想法吗?

Nee*_*eet 5

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

InputStream in = new FileInputStream(cwd + 
    file_separator + "data" + file_separator + "configuration.properties");
Run Code Online (Sandbox Code Playgroud)