将properties文件放在Jar包并读取


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

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

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

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

一切OK.

相关内容