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. iOS组件化之-给自己的组件添加资源文件

    在 podspec 中,利用 source_files 可以指定要编译的源代码文件.可是,当我们需要把图片.音频.NIB等资源打包进 Pod 时该怎么办呢? 1.如何把资源文件打包为.bundle文件 ...

  2. IP分组和分片

    本文讨论两个问题①IP数据报的首部②IP数据报的分片 TCP/IP模型分为五层,从上到下依次是应用层.传输层.网络层.数据链路层和物理层. IP数据报是网络层的概念. IP数据报的首部 版本号:0~3 ...

  3. Solution -「ARC 124E」Pass to Next

    \(\mathcal{Description}\)   Link.   有 \(n\) 个人站成一个环,初始时第 \(i\) 个人手里有 \(a_i\) 个球.第 \(i\) 个人可以将自己手中任意数 ...

  4. 二、MyBatis基础配置应用实例

    核心配置文件sqlMapConfig.xml Mybatis核心配置文件层级关系 1)environments标签 2)mapper标签 eg: 3)properties标签 数据源配置参数抽取至jd ...

  5. 实测Tengine开源的Dubbo功能

    本文已收录 https://github.com/lkxiaolou/lkxiaolou 欢迎star. 搜索关注微信公众号"捉虫大师",后端技术分享,架构设计.性能优化.源码阅读 ...

  6. HDFS免重启挂载新磁盘

    背景 在生产环境中,集群节点磁盘大小不同,其使用率也会不同,HDFS虽有均衡策略,但也会有数据不平衡的情况,有些节点磁盘就会被打满,然后这个节点就不健康了(Unhealthy Nodes),Yarn的 ...

  7. 学习Spring5必知必会(6)~Spring DAO

    一.Spring 对持久层技术的支持 Spring DAO 1.模板类: 2.基类: 二.spring JDBC [JDBCTemplate 模板类] 1.案例:使用jdbc 完成crud操作 (1) ...

  8. DT时代,优秀的BI工具应该具备哪些功能

    马云曾在一次演讲中说:"人类正从IT时代走向DT时代."那DT究竟是什么,和IT有什么不同呢?我们对IT非常熟悉,它是信息技术(InformationTechnology)的英文缩 ...

  9. Smartbi:利用好excel分析工具,数据分析都是小case!

    数据分析听起来好像很高端的样子,但是实际上在一些IT高手的眼里,只需要掌握以下几个excel数据分析小工具的使用,你也能够成为别人眼中的数据大神! 1.excel数据分析工具--条件格式 快速找出符合 ...

  10. 微信小程序使用weui扩展组件踩坑

    最近在做微信小程序,引入weui的时候踩坑了好久,这里记录一下遇到的问题. 微信官方文档给了两种weui引入方式: 通过 useExtendedLib 扩展库 的方式引入,这种方式引入的组件将不会计入 ...