2016-08-03 68 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