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 に答える