2016-08-03 67 views
1

我正在使用maven web应用程序。 以下是我放置我的属性文件的项目结构。

enter image description here未找到Maven属性文件

以下是我的代码,我使用的读取这个文件:

public static Properties loadProducerProperty() throws FileException { 
    Properties myProperties = new Properties(); 

    try { 
     String resourceName = "newproperties.properties"; // could also be a constant 
     ClassLoader loader = Thread.currentThread().getContextClassLoader(); 
     try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) { 
      myProperties.load(resourceStream); 
     } 


    } catch (FileNotFoundException e) { 
     throw new FileException(); 
    } catch (IOException e) { 
     throw new FileException(); 
    } 

    return myProperties; 
} 

但我正在逐渐FileNotFound异常

我已经通过下面的链接去和尝试过其他的东西也但我得到相同的错误:

Cannot load properties file from resources directory

我在做什么错在这里。

谢谢

回答

2

添加一条斜线在文件名前:

String resourceName = "/newproperties.properties"; // could also be a constant