2016-10-17 20 views
0

我的類中的靜態方法必須讀取位於另一個模塊中的屬性文件。在不同模塊中讀取屬性文件

public class Util 
{ 
    private static void readProp() 
    { 
    Properties prop = new Properties(); 
    String fileName = "/appconfig.properties"; //File in another module 
    InputStream inputStream = null; 
    try 
    { 
     inputStream = ClassLoader.getSystemResourceAsStream(propFileName); 
     if (inputStream != null) 
     { 
     prop.load(inputStream); 
     } 
    } 
      catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 
} 

Util方法在module1中,而appconfig.properties在module2中。

兩個絕對路徑是

Util.java:

/Users/user1/IdeaProjects/myProject/module1/src/main/java/com/microsoft/e3/cx/service/windows/search/util/Util.java 

appconfig.properties:

/Users/user1/IdeaProjects/myProject/module2/appconfig/base/appconfig.properties 

模塊2的POM具有MODULE1作爲依賴

<dependency> 
    <groupId>microsoft.module1</groupId> 
    <artifactId>module1</artifactId> 
    <version>${project.version}</version> 
</dependency> 

我的inputStream總是空出來。我想這是因爲它無法找到這個文件。對於如何解決這個問題,有任何的建議嗎?

回答

0

嘗試,

Util.getClass().getResourceAsStream("appconfig.properties"); 
+0

Util.getClass()難道不在靜態環境中工作。該方法是靜態的 –