简体   繁体   English

输入流为 null

[英]InputStream is null

In a program I'm working on I have在我正在研究的程序中

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();
}

For some reason, though, c.load(in);但是,出于某种原因, c.load(in); causes a NullPointerException .导致NullPointerException The exception comes from in == NULL being true.异常来自in == NULL为真。 I can't figure out why because我不明白为什么因为

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

prints印刷

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

which is the location of the file I'm wanting to use.这是我要使用的文件的位置。

Thoughts?想法?

getResourceAsStream is meant to search for files on the classpath and not for accessing the local file system. getResourceAsStream用于搜索类路径中的文件,而不是用于访问本地文件系统。 You will have to use FileInputStream for this case.在这种情况下,您将不得不使用FileInputStream

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM