2009-04-20 115 views
6

我試圖加載屬性文件而不使用文件的實際路徑。我已經在其他一些簡單的應用程序中使用過:classloader.getSystemResourceAsStream返回null

InputStream inputStream = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE); 
props.load(inputStream); 

但是這次它不起作用。由於某種原因,inputStream爲空。 PROPERTIES_FILE是一個常量,被定義爲「app.properties」。我試圖刪除.properties擴展名並得到相同的結果。

任何想法?

謝謝。

+2

app.properties位於何處? – 2009-04-20 21:13:39

回答

13

的PROPERTIES_FILE常數應包括包,以及屬性文件(例如,「COM /一些/庫/ file.properties」。

final static String PROPS_FILE = "/com/some/library/file.props"; 
        //The preceding "/" is dependendant on wheterh 
        //you are going to be giving a relative or absolute location 
    InputStream is = YourCurrentClass.class.getResourceAsStream(PROPS_FILE); 
+0

我已經添加了包提示信息,但仍然出現同樣的錯誤。如果這樣做有助於任何... – Lancelot 2009-04-20 21:20:56