1.读取配置文件的键值对,转为Properties对象;将Properties(键值对)对象写入到指定文件。

package com.ricoh.rapp.ezcx.admintoolweb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class PropertiesFileHandle { private static Logger logger = LoggerFactory.getLogger(PropertiesFileHandle.class); public static Properties readProperties(String filePath) {
String realPath = FileUtil.getEzChargerInstallPath() + filePath; Properties props = new Properties();
File configFile = new File(realPath);
logger.debug("#configFile: " + configFile.getAbsolutePath());
InputStream fis = null;
try {
fis = new FileInputStream(configFile);
props.load(fis);
} catch (IOException e) {
logger.error("readProperties failed in" + realPath + ". "+ e.toString());
return null;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.debug("readProperties close file failed." + e.toString());
}
}
return props;
} public static boolean writeProperties(String filePath, Properties prop) {
String realPath = FileUtil.getEzChargerInstallPath() + filePath; File configFile = new File(realPath);
if(!configFile.exists()) {
configFile.getParentFile().mkdirs();
try {
configFile.createNewFile();
} catch (IOException e) {
logger.error("PropertiesFileHandle.writeProperties failed. because create file[" + realPath
+ "]. is IOException:"+ e.getMessage());
e.printStackTrace();
return false;
}
}
InputStream fis = null;
OutputStream fos = null;
try {
fos = new FileOutputStream(configFile);
prop.store(fos, "");
} catch (Exception e) {
logger.error("WriteProperties failed in" + realPath + ". "+ e.toString());
return false;
} finally {
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.debug("writeProperties close file failed." + e.toString());
}
}
return true;
}
}

2.通过输入流或者Properties对象将Properties文件的内容读取到map集合中。

package com.ricoh.rapp.ezcx.edcactivation.internal;

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.Map.Entry; public class PropertyUtil {
public static Map<String, String> loadPropertiesFile(InputStream file) {
HashMap result = new HashMap(); try {
Properties prop = new Properties();
prop.load(file);
Iterator var4 = prop.entrySet().iterator(); while (var4.hasNext()) {
Entry<Object, Object> entry = (Entry) var4.next();
result.put((String) entry.getKey(), (String) entry.getValue());
}
} catch (IOException var5) {
System.out.println("faild load properties file .");
} return result;
} public static Map<String, String> loadPropertiesFile(Properties prop) {
Map<String, String> result = new HashMap();
if (prop == null) {
return result;
} else {
Iterator var5 = prop.entrySet().iterator(); while (var5.hasNext()) {
Entry<Object, Object> entry = (Entry) var5.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (key != null && key.length() > 0 && value != null && value.length() > 0) {
result.put(key, value);
}
} return result;
}
}
}

3.实例使用1和2的方式来处理Properties文件

private void innitPropreties() {
Map<String, String> rsiConfMap = new HashMap<>();
Properties proxyProp = PropertiesFileHandle.readProperties("/conf/test.properties");
if (proxyProp != null) {
rsiConfMap = PropertyUtil.loadPropertiesFile(proxyProp);
}else {
rsiConfMap.put("key1", "value1");
rsiConfMap.put("key2", "value2");
Properties properties = new Properties();
properties.put("key1", "value1");
properties.put("key2", "value2");
PropertiesFileHandle.writeProperties("/conf/test.properties", properties);
} String value1= rsiConfMap.get("key1");
String value2= rsiConfMap.get("key2");
}

java对配置文件properties的操作的更多相关文章

  1. Java读写配置文件——Properties类的简要使用笔记

    任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...

  2. java读取配置文件(properties)的时候,unicode码转utf-8

    有时我们在读取properties结尾的配置文件的时候,如果配置文件中有中文,那么我们读取到的是unicode码的中文,需要我们在转换一下,代码如下 /** * 将配置文件中的Unicode 转 ut ...

  3. Java 读取配置文件 Properties

    String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new Buff ...

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

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

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

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

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

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

  7. 关于Java配置文件properties的学习

    在Java早期的开发中,常用*.properties文件存储一些配置信息.其文件中的信息主要是以key=value的方式进行存储,在早期受到广泛的应用.而后随着xml使用的广泛,其位置渐渐被取代,不过 ...

  8. java文件操作(普通文件以及配置文件的读写操作)

    转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...

  9. 操作配置文件Properties

    // */ // ]]>   操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...

随机推荐

  1. 帆软报表(finereport) 组合地图 保持系列名和值居中

    自定义JavaScript代码,使用HTML解析 function(){ var name = this.name; var total = '<div style="width:10 ...

  2. Solution -「USACO 2020.12 P」Spaceship

    \(\mathcal{Description}\)   Link.   Bessie 在一张含 \(n\) 个结点的有向图上遍历,站在某个结点上时,她必须按下自己手中 \(m\) 个按钮中处于激活状态 ...

  3. MySQL中的严格模式

    很多集成的PHP环境(PHPnow WAMP Appserv等)自带的MySQL貌似都没有开启MySQL的严格模式,何为MySQL的严格模式,简单来说就是MySQL自身对数据进行严格的校验(格式.长度 ...

  4. QQ表情代码大全,你知道几个??

    很久没有给大家分享代码了,今天趁着有点时间来给大家分享一下QQ空间的表情代码!不用感谢我,大家拿去用吧! [em]e100[/em] 微笑bai[em]e101[/em] 撇嘴[em]e102[/em ...

  5. vivo全球商城全球化演进之路——多语言解决方案

    一.背景 随着经济全球化的深入,许多中国品牌纷纷开始在海外市场开疆扩土.实现全球化意味着你的产品或者应用需要能够在全球各地的语言环境使用,我们在进行海外业务的推进时,需要面对的最大挑战就是多语言问题. ...

  6. Oracle分析函数/排名函数/位移函数/同比环比

    分析函数 作用:分析函数可以在数据中进行分组,然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计值.统计函数:MAX(字段名).MIN(字段名).AVG(字段名).SUM(字段名).CO ...

  7. Hadoop - 入门学习笔记(详细)

    目录 第1章 大数据概论 第2章 从Hadoop框架讨论大数据生态 第3章 Hadoop运行环境搭建(开发重点) 第4章 Hadoop运行模式 本地模式:默认配置 伪分布式模式:按照完全分布式模式配置 ...

  8. vmware启动报错:Failed to load SELinux policy. Freezing

    修改 : SELINUX=disabled     正确 误修改: SELINUXTYPE=disabled   错误 导致无法开机 错误结果 重启后 机器就报 Failed to load SELi ...

  9. NPOI导出大量数据的避免OOM解决方案【SXSSFWorkbook】

    一.NPOI的基本知识 碰到了导出大量数据的需求场景:从数据读取数据大约50W,然后再前端导出给用户,整个过程希望能较快的完成.如果不能较快完成,可以给与友好的提示. 大量数据的导出耗时的主要地方: ...

  10. JavaWeb-前端基础

    前端基础 前端:我们网站的页面,包括网站的样式.图片.视频等一切用户可见的内容都是前端的内容. 后端:处理网站的所有数据来源,比如我们之前从数据库中查询数据,而我们查询的数据经过处理最终会被展示到前端 ...