将peoperties文件的读取和写入封装成了一个工具类:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class PropertyUtil { /**
* 读取properties文件
* @param fileName
* @return
*/
public static Map<String, String> getProperties(String fileName) {
Map<String, String> propertyMap = new HashMap<String, String>();
Properties properties = new Properties();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(fileName));
properties.load(in);
Iterator<String> itr = properties.stringPropertyNames().iterator();
while (itr.hasNext()) {
String key = itr.next();
String value = properties.getProperty(key);
propertyMap.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return propertyMap;
} /**
* 将properties保存成文件
* @param properties
* @param fileName
* @param comments
*/
public static void saveProperties(Map<String, String> properties,
String fileName, String comments) {
Properties property = new Properties();
FileOutputStream out = null;
try {
out = new FileOutputStream(fileName);
Set<Map.Entry<String, String>> entrySet = properties.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue();
property.setProperty(key, value);
}
property.store(out, comments);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

properties文件读取与写入的更多相关文章

  1. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  2. 对Java配置文件Properties的读取、写入与更新操作

    http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties  对Jav ...

  3. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  4. 实现对Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  5. properties文件读取

    package properties; import java.io.FileInputStream; import java.io.FileNotFoundException; import jav ...

  6. python文件读取和写入案例

    python文件读取和写入案例  直接上代码吧 都是说明 百度上找了很多,最终得出思路 没有直接可以读取修改的扩展,只能先读取,然后复制一份,然后在复制出来的文件里面追加保存 然后删除读的那个,但是缺 ...

  7. SpringMVC中properties文件读取

    SpringMVC给我们提供了用于properties文件读取的类: org.springframework.context.support.ResourceBundleMessageSource 1 ...

  8. Java底层代码实现单文件读取和写入(解决中文乱码问题)

    需求: 将"E:/data/车站一次/阿坝藏族羌族自治州.csv"文件中的内容读取,写入到"E:/data//车站一次.csv". 代码: public cla ...

  9. Apache commons-io实现单文件读取和写入

    Apache commons-io提供了很多类,这里只介绍FileUtils类. FileUtils类提供了一些操作文件对象的实用方法,包括文件的读取.写入.复制和比较等. 比如逐句读取和写入一个文件 ...

随机推荐

  1. [TJOI2013]拯救小矮人[排序+dp]

    题意 题目链接 分析 Imagine的完美回答 重点大概是证明我们选出要救的小矮人一定可以根据 \(a_i+b_i\) 的大小进行排序救出. 注意这里关注的对象是可以保留的高度,所以我们的dp值才会表 ...

  2. Object Relational Mapping框架之Hibernate

    hibernate框架简介: hibernate框架就是开发中在持久层中应用居多的ORM框架,它对JDBC做了轻量级的封装. (百度介绍,感觉不错) 什么是ORM:Object Relational ...

  3. PCB设计铜箔厚度、线宽和电流关系

    PCB的载流能力取决于一下因素:线宽.线厚.容许温升.在同等条件下,假设10mil的走线能承受1A,那么50mil的走线能承受的电流却不是5A. 如下: 1 盎司 = 0.0014英寸 = 0.035 ...

  4. (转)CentOS7系统信息及运行情况查看

    原文链接:https://blog.csdn.net/qq_42196196/article/details/85063911 系统信息 CPU信息 内存信息 显卡信息 硬盘信息 网络信息 用户信息 ...

  5. 基于Vue的简单通用分页组件

    分页组件是每一个系统里必不可少的一个组件,分页组件分为两部分.第一部分是模版部分,用于显示当前分页组件的状态,例如正在获取数据.没有数据.没有下一页等等:第二部分是分页数据对象,用于封装一个分页组件的 ...

  6. selenium的基本定位方式总结

    Selenium提供了8种定位方式. id name class name tag name link text partial link text xpath css selector 这8种定位方 ...

  7. 用 Delphi 7 实现基于 FFMS2 的视频转 GIF 工具 [原创]

    儿子经常要把自拍的视频(ts格式)转成表情包,下载了几个工具都不大好用,更多的还要收费.那就想自己写一个吧,没想到这一下断断续续地,居然 3 个月过去了.现在总算弄出个作品来了,结个贴吧.唉,天资愚钝 ...

  8. swap分区和内存

    1  查看swap 空间大小(总计):      # free -m          默认单位为k, -m 单位为M                total       used       fr ...

  9. PHP字符编码转换库iconv的一个细节

    先来看代码 <?php $charset = 'GBK'; $str = '中华人民共和国中华人民共和国中华人民共和国中华人民共和国'; ; $str2 = iconv('UTF-8', $ch ...

  10. 满帮集团CEO:未来将向“智慧型”公司转变,要成为一家生态公司

    谁都想成为下一个滴滴.显然,王刚也希望在物流业,货车帮与运满满在合并后,能够企及滴滴的高度. 货车帮与运满满,都曾是货运物流领域的翘楚,也因为业务的竞争关系有过水火不容厮杀.但最终还是在资本与地方政府 ...