将资源放在java项目中的位置以及如何从资源中读取资源

时间:2015-03-21 17:28:05

标签: java

我正在尝试从我的java项目中读取资源,我已将属性文件放在resource / properties / name.properties中。以下是我到目前为止返回的代码,想要从相对路径中读取。

public void loadResource()
{
    Properties config = new Properties();
    final String dir = System.getProperty("user.dir");
    System.out.println("current dir = " + dir);
    InputStream in = getClass().getResourceAsStream(dir+"resource/properties/name.properties"); 
    try
    {
        config.load(in);
    }
    catch(IOException ex)
    {

1 个答案:

答案 0 :(得分:0)

始终从类路径加载资源。

如果指定相对路径,则相对于类,否则在类路径(不是磁盘)上为绝对路径。

您的程序不正确,您应该使用:

getClass().getResourceAsStream("/resource/properties/name.properties");
相关问题