classloader.getSystemResourceAsStream返回null

Lan*_*lot 6 java properties classloader

我正在尝试加载属性文件而不使用文件的实际路径.我已经使用以下方法在其他一些简单的应用程序上完成了

InputStream inputStream = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE);
props.load(inputStream);
Run Code Online (Sandbox Code Playgroud)

但这一次它不起作用.由于某种原因,inputStream为null.PROPERTIES_FILE是一个定义为"app.properties"的常量.我试图删除.properties扩展并获得相同的结果.

有任何想法吗?

谢谢.

jco*_*lin 13

PROPERTIES_FILE常量应包括包和属性文件(例如"com/some/library/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);
Run Code Online (Sandbox Code Playgroud)