将properties文件放在Jar包并读取

字体大小: 中小 标准 ->行高大小: 标准
有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部。
 
在maven工程里面,将properties文件放在src/main/resources目录下。就会自动打包到classes目录下。
 
然后在jar包的代码中想要读取这个文件,可以用类似下面的代码:
 
public class Client {  

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

一切OK.

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/68272.html