Java加载Properties属性文件 电玩女神 2023-08-17 17:31 83阅读 0赞 /\*\* \* 加载Properties属性文件 \* \* @param propFilePath Properties属性文件相对classpath的路径 \* @param encode Properties属性文件编码 \* @return Properties对象 \*/ public static Properties loadProperties(String propFilePath, String encode) \{ if (StringUtils.isBlank(propFilePath)) \{//StringUtils 工具类来做字符串的操作,判断文件路径是否为空 return null; \} Properties prop = null; InputStream in = null; try \{ /\*path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。\*/ in = PropertiesUtil.class.getResourceAsStream(propFilePath); /\*Properties的load方法其实就是传进去一个输入流,字节流或者字符流,字节流利用InputStreamReader转化为字符流,然后字符流用BufferedReader包装,BufferedReader读取properties配置文件,每次读取一行,分割成两个字符串。因为Properties是Map的子类,然后用put将两个字符串装进Properties对象。\*/包装了Reader reader = new InputStreamReader(in, encode); prop = loadProperties(in, encode);//loadProperties为自定义函数 \} catch (Exception e) \{ prop = null; LOGGER.error("loadProperties - Excp : \{\}; propFilePath = \{\}", e, propFilePath); \} finally \{ if (in != null) \{ try \{ in.close(); \} catch (IOException e) \{ \} \} \} return prop; \}
还没有评论,来说两句吧...