将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. linux 利用cat写入一段文件

    linux 利用cat写入一段文件 cat >> /etc/rc.local <<EOFsysctl -w net.ipv4.icmp_echo_ignore_all=1sys ...

  2. nginx解析漏洞,配置不当,目录遍历漏洞环境搭建、漏洞复现

    nginx解析漏洞,配置不当,目录遍历漏洞复现 1.Ubuntu14.04安装nginx-php5-fpm 安装了nginx,需要安装以下依赖 sudo apt-get install libpcre ...

  3. Vue2.0原理-模板解析

    下面这段代码,vue内部做了什么操作?我去源码里面找找看 new Vue({ el: '#app' }) 入口 vue 的入口文件在 src/core/instance/index.js, 里面一进来 ...

  4. WebGL——水波纹特效

    大家好,今天我ccentry要做一个水波纹特效,我们来看看水波纹特效的做法.首先我们来看一下水波纹特效的效果是怎么样的,请看下图. 我们要做的就是类似这种纹理特效,那么我们来看看是如何制作的吧.首先鲫 ...

  5. Linux文件句柄数调整

    首先介绍下Linux系统中"一切都是文件". 1. Linux系统文件句柄数概念 文件句柄(Windows) 文件描述符(Unix/Linux):file discriptor,f ...

  6. python—索引与切片总结

    python中索引与切片的熟练掌握对于字符串的操作很有帮助,梳理如下: (1)索引 S = 'hello world' 1)正向索引 正向索引从0开始,向右依次递增. 2)反向索引 反向索引从-1开始 ...

  7. SSZipArchive解压失败的原因

    SSZipArchive 解压失败的原因是路径不对解压失败后,修正路径后再次解压就会出现解压失败的情况,代理方法也不会走也没有日志输出.解决方法就是删除手机上的软件再次运行就可以了.这个问题也就在内部 ...

  8. hive创建临时函数

    add jar /home/hjl/sunwg/Lower.jar; create temporary function my_lower as ‘com.example.hive.udf.Lower ...

  9. Data truncation: Truncated incorrect DOUBLE value:

    在写sql查询语句queryRunner.update(connection,"update account set balance=? where name=?",account ...

  10. 11.15 Daily Scrum

    今天是假期回来的第一个周末,也是我们团队的又一次进度汇总总结和调试工作开展,鉴于一周以来大家的工作有了很大的成果,所以,本次召开的会议主旨在于解决一些开发方面的细节问题,达成共识,为日后进一步的功能方 ...