PropertiesUtil,读取properties文件
来源:互联网 发布:yum 安装ant 编辑:程序博客网 时间:2024/06/16 05:38
public class PropertiesUtil { private static HashMap<String, String> configMap; private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); public static void init(String path) { // 把config.properties配置文件中的配置信息放入到Map中 configMap = new HashMap<String, String>(); Properties p = new Properties(); try { //获取要加载的文件 p.load(new FileInputStream(new File(getClassPath() + path))); } catch (IOException e) { logger.error("init properties file excetion", e); } Set<Object> keySet = p.keySet(); for (Object key : keySet) { configMap.put(key.toString(), p.getProperty(key.toString())); } } /** * 从ClassPath中的读取属性的名称 * * @param fileName 系统文件名称,在classpath下"#.properties" * @param propertyName 属性名称 * @return */ public static String getPropertys(String fileName, String propertyName) { String value = ""; try { Properties props = PropertiesLoaderUtils.loadAllProperties(fileName); value = props.getProperty(propertyName); } catch (IOException e) { logger.debug("file not found ", e); } return value; } /** * 获取系统文件classpath路径 * * @return */ public static String getClassPath() { return PropertiesUtil.class.getClassLoader().getResource("").getPath().replaceAll("%20", " "); } /** * 设置系统配置文件值 * * @param fileName * 系统文件名称,在classpath下"#.properties" * @param map * 含有配置文件中属性的键值对儿 */ public static boolean setPropertyFile(String fileName, Map<String, String> map) { Properties props = new Properties(); InputStream in = null; OutputStream out = null; try { in = new FileInputStream(getClassPath() + fileName); props.load(in); out = new FileOutputStream(getClassPath() + fileName); Iterator<Entry<String, String>> iterator = map.entrySet() .iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); String key = entry.getKey().toString(); String value = entry.getValue().toString(); props.setProperty(key, value); } props.store(out,null); return true; } catch (FileNotFoundException e) { logger.debug("file not found ", e); } catch (IOException e) { logger.debug("can't get stream", e); } finally { try { if (null != in) { in.close(); } if (null != out) { out.close(); } } catch (IOException e) { logger.debug("can't close stream", e); } } return false; } /** * 设置系统配置文件值 * * @param dir * 根据路径找到properties文件 * @param map * 含有配置文件中属性的键值对儿 */ public static boolean setPropertyDirFile(String dir, Map<String, String> map) { Properties props = new Properties(); InputStream in = null; OutputStream out = null; try { in = new FileInputStream(dir); props.load(in); out = new FileOutputStream(dir); Iterator<Entry<String, String>> iterator = map.entrySet() .iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); String key = entry.getKey().toString(); String value = entry.getValue().toString(); props.setProperty(key, value); } props.store(out, null); return true; } catch (FileNotFoundException e) { logger.debug("file not found ", e); } catch (IOException e) { logger.debug("can't get stream", e); } finally { try { if (null != in) { in.close(); } if (null != out) { out.close(); } } catch (IOException e) { logger.debug("can't close stream", e); } } return false; } /** * 根据key得到配置项的值 * * @param key * @return */ public static String getProperty(String path, String key) { init(path); return configMap.get(key); } /** * 根据提供po文件 路径和key得到值 * * @param path * @param key * @return */ public static String getPropertyByPath(String path, String key) { configMap = new HashMap<String, String>(); Properties p = new Properties(); try { p.load(new FileInputStream(new File(path))); } catch (IOException e) { logger.error("init properties file excetion", e); } Set<Object> keySet = p.keySet(); for (Object key1 : keySet) { configMap.put(key1.toString(), p.getProperty(key1.toString())); } return configMap.get(key); } /** * 根据提供po文件 路径得到键值对 * * @param path * @param key * @return */ public static Map<String, String> getPropertyByPath(String path) { configMap = new HashMap<String, String>(); Properties p = new Properties(); try { p.load(new FileInputStream(new File(path))); } catch (IOException e) { logger.error("init properties file excetion", e); } Set<Object> keySet = p.keySet(); for (Object key1 : keySet) { configMap.put(key1.toString(), p.getProperty(key1.toString())); } return configMap; }}
阅读全文
0 0
- PropertiesUtil,读取properties文件
- 读取配置文件ProPertiesUtil
- Properties读取properties文件
- Properties 读取 Properties文件
- Properties文件读取 Message.properties
- propertiesUtil
- PropertiesUtil
- propertiesUtil
- PropertiesUtil
- PropertiesUtil
- 读取中文Properties文件
- properties 文件的读取
- C#读取properties文件
- Java读取Properties文件
- java读取.properties文件
- struts读取.properties文件
- ejb读取properties文件
- 读取properties文件-PropertyResourceBundle
- webpack.config.js 参数详解
- 呕心沥血,拒绝水货,Jsp结和js做前端开发经验<二>
- dll获取自身路径的几种方法
- maven 发布项目GBK编码格式不可映射
- Codeforces-808E
- PropertiesUtil,读取properties文件
- 限制应用访问网络
- 并发编程笔记(一):并发编程的挑战和解决方案
- windwos下连接安卓模拟器的命令
- 全面掌握Java内部类
- 【CTSC2012】bzoj2806 Cheat
- 分布式缓存应用
- Android AsynTask 分析
- StringBuffer的用法