将properties文件放在Jar包并读取

时间:2023-03-09 22:51:30
将properties文件放在Jar包并读取

有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部。

在maven工程里面,将properties文件放在src/main/resources目录下。就会自动打包到classes目录下。

然后在jar包的代码中想要读取这个文件,可以用类似下面的代码:

  1. public class Client {
  2. private final static String NAME = "engine.properties";
  3. public static Map<String, String> load() throws AppEngineException {
  4. InputStream stream = Client.class.getClassLoader().getResourceAsStream(NAME);
  5. ...
  6. }
  7. }

一切OK.

转自:http://blog.****.net/csfreebird/article/details/7701953