Java操作properties文件
2014-02-16 15:42
369 查看
/** * 根据key读取value * @param filePath * @param key * @return */ public static String readValue(String filePath, String key) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); String value = props.getProperty(key); return value; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 读取properties的全部信息 * @param filePath */ public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); Enumeration<?> en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key); System.out.println(key + Property); } } catch (Exception e) { e.printStackTrace(); } } /** * 写入properties信息 * @param filePath * @param parameterName * @param parameterValue */ public static void writeProperties(String filePath, String parameterName, String parameterValue) { Properties prop = new Properties(); try { InputStream fis = new FileInputStream(filePath); // 从输入流中读取属性列表(键和元素对) prop.load(fis); // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性 // 强制要求为属性的键和值使用字符串,返回值是 Hashtable 调用 put 的结果 OutputStream fos = new FileOutputStream(filePath); prop.setProperty(parameterName, parameterValue); // 以适合使用 load 方法加载到 Properties 表中的格式 // 将此 Properties 表中的属性列表(键和元素对)写入输出流 prop.store(fos, "Update '" + parameterName + "' value"); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Eclipse安装插件的方法
- Java去除集合List内重复记录
- Struts 2版验证码
- Java计算字符在字符串内出现的次数
- win7 + maven + nexus + Eclipse
- Apriori算法实例----Weka,R, Using Weka in my javacode
- Myeclipse中java项目转成Web项目
- spring,mybatis事务管理配置与@Transactional注解使用
- Java身份证验证工具类
- Eclipse Debug模式的开启与关闭
- JAVA基础加强之枚举
- java类加载器
- java传智播客入学题目总结
- JAVA OOP 基础知识提纲
- Java 默认修饰符 总结
- java并发编程学习总结(基础篇)
- java post数据服务器拒绝的解决方法
- java String类(字符串)的方法
- java 类的加载,与获得相应的方法
- java打印当前方法名示例分享