博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java操作properties文件
阅读量:7120 次
发布时间:2019-06-28

本文共 2814 字,大约阅读时间需要 9 分钟。

1     /** 2      * 向properties文件写入属性 3      *  4      * @param filePath 5      * @param parameterName 6      * @param parameterValue 7      */ 8     public static void writePropertiesFile(String filePath, String parameterName, String parameterValue) { 9         Properties prop = new Properties();10         try {11             InputStream fis = new FileInputStream(filePath);12             // 从输入流中读取属性列表(键和元素对)13             prop.load(fis);14             // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。15             // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。16             OutputStream fos = new FileOutputStream(filePath);17             prop.setProperty(parameterName, parameterValue);18             // 以适合使用 load 方法加载到 Properties 表中的格式,19             // 将此 Properties 表中的属性列表(键和元素对)写入输出流20             prop.store(fos, "Update '" + parameterName + "' value");21         } catch (IOException e) {22             System.err.println("Visit " + filePath + " for update " + parameterName + " value error");23         }24     }25 26     /**27      * 读取properties文件内容28      * 29      * @param filePath30      * @param parameterName31      * @return32      */33     public static String readProperties(String filePath, String parameterName) {34         Properties prop = new Properties();35         try {36             InputStream fis = new FileInputStream(filePath);37             // 从输入流中读取属性列表(键和元素对)38             prop.load(fis);39             return String.valueOf(prop.get(parameterName));40         } catch (IOException e) {41             System.err.println("Visit " + filePath + " for query " + parameterName + " value error");42         }43         return null;44     }

使用prop.load()和prop.store()读取和写入properties文件时, 会出现乱序的问题,因为Properties继承于<,>, 另外之前的注释也会丢失.可改用直接当成文本文件处理,

1 /** 2      * 向properties文件写入属性. 由于使用prop.load(fis)读取properties文件内容后再写入会乱序, 改用直接操作文件 3      *  4      * @param filePath 5      * @param parameterName 6      * @param parameterValue 7      */ 8     public static void writePropertiesFile(String filePath, String parameterName, String parameterValue) { 9         try {10             File file = new File(filePath);11             List
newLines = new ArrayList
();12 for (Object line : FileTools.readLines(file)) {13 if (line.toString().startsWith(parameterName + "=")) {14 newLines.add(parameterName + "=" + parameterValue);15 } else {16 newLines.add(line.toString());17 }18 }19 FileTools.writeLines(file, newLines);20 } catch (IOException e) {21 System.err.println("Visit " + filePath + " for update " + parameterName + " value error");22 }23 }

 

 

转载于:https://www.cnblogs.com/jimor/p/3414091.html

你可能感兴趣的文章
如果要学习web前端,需要学习什么
查看>>
《编写高质量代码:改善Java程序的151个建议》笔记
查看>>
LeetCode每日一题: 搜索插入位置(No.35)
查看>>
利用装饰器实现mock和api的局部分离切换
查看>>
-[UIView hitTest:withEvent:] 方法总结
查看>>
springboot
查看>>
ios设计规范(下)
查看>>
Git-github 的基本应用
查看>>
前端基础8:HTML5新增标签及CSS3新属性 viewport 动画
查看>>
GitHub+Hexo 搭建个人网站
查看>>
使用BitmapFactory压缩图片遇到的问题总结
查看>>
vue指令和特殊特性
查看>>
Wings-让单元测试智能全自动生成
查看>>
LAMP(CentOS 7.2)环境下搭建WordPress
查看>>
css几个居中的方法
查看>>
程序员如何从0到1搭建自己的技术博客
查看>>
【Spring Boot 实战】数据库千万级分库分表和读写分离实战
查看>>
势高,则围广:TiDB 的架构演进哲学
查看>>
移动浏览器的四大内核
查看>>
springmvc+mybatis+dubbo+zookeeper分布式架构
查看>>