2009-04-20 116 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