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. linux 定时删除图以及crontab介绍

    执行 sudo crontab -e 0 3 1 * * /etc/letsencrypt/certbot-auto renew --renew-hook "sudo nginx -s re ...

  2. elasticsearch按URL查询

    排序查询:localhost:9200/get-together/_search?sort=date:asc, 排序以及按字段查询:localhost:9200/get-together/_searc ...

  3. Keras学习:试用卷积-训练CIFAR-10数据集

    import numpy as np import cPickle import keras as ks from keras.layers import Dense, Activation, Fla ...

  4. Solution -「CTS 2019」「洛谷 P5404」氪金手游

    \(\mathcal{Description}\)   Link.   有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...

  5. Solution -「LOCAL」画画图

    \(\mathcal{Description}\)   OurTeam.   给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...

  6. Solution -「POI 2010」「洛谷 P3511」MOS-Bridges

    \(\mathcal{Description}\)   Link.(洛谷上这翻译真的一言难尽呐.   给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...

  7. shell脚本批量配置多台主机静态ip

    关于脚本 服务器使用之前,都需要先配置静态IP,那就将这种简单重复的工作,交给脚本来处理吧,让我们运维有更多的时间喝茶看报刷微博 脚本使用 sh ssh.sh ip.txt ssh.sh 为脚本的名称 ...

  8. CentOS8系统上Zookeeper安装启动

    下载Zookeeper安装包,下载地址为:https://zookeeper.apache.org/releases.html.注意选择bin的gz包,否则安装不成功,提示找不到相应的类. 一.单机模 ...

  9. SpringBoot整合Redis案例缓存首页数据、缓解数据库压力

    一.硬编码方式 1.场景 由于首页数据变化不是很频繁,而且首页访问量相对较大,所以我们有必要把首页数据缓存到redis中,减少数据库压力和提高访问速度. 2.RedisTemplate Jedis是R ...

  10. 理解OAuth2.0协议和授权机制

    无论是自然资源还是互联网上的资源,需要控制使用权与被使用权,以保护资源的安全.合理的使用和有效的管控. 项目中,我们需要控制的是用户资源,既要保证有效用户的合理使用,又要防范非法用户的攻击.如此,如何 ...