读取 Maven resources 包下的配置文件

版权声明: https://blog.csdn.net/weixin_39823527/article/details/86300754

 读取resources下的文件:

在maven工程中,我们会将配置文件放到src/main/resources下面。

它编译的路径直接位于classes下面,这个路径其实就是classPath的路径,所以在resource根目录下的配置文件其实就是classPath的路径

public class SecurityVerificationCore {
	

	/**
	 * 读取配置文件
	 * @param key
	 * @return
	 * @throws IOException 
	 */
	public static String readTokenProperties(String key) throws IOException{
//		ResourceBundle resource = ResourceBundle.getBundle("com/velcro/receivable/base/service/fKBCilent");
//		String result = resource.getString(key);
		InputStream in = SecurityVerificationCore.class.getResourceAsStream("/fKBCilent.properties");
		Properties props = new Properties();
		InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
		props.load(inputStreamReader);
		String result = props.getProperty(key);
		return result;
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/weixin_39823527/article/details/86300754