java对配置文件properties的操作
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的操作的更多相关文章
- Java读写配置文件——Properties类的简要使用笔记
任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...
- java读取配置文件(properties)的时候,unicode码转utf-8
有时我们在读取properties结尾的配置文件的时候,如果配置文件中有中文,那么我们读取到的是unicode码的中文,需要我们在转换一下,代码如下 /** * 将配置文件中的Unicode 转 ut ...
- Java 读取配置文件 Properties
String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new Buff ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 对Java配置文件Properties的读取、写入与更新操作
http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties 对Jav ...
- 实现对Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 关于Java配置文件properties的学习
在Java早期的开发中,常用*.properties文件存储一些配置信息.其文件中的信息主要是以key=value的方式进行存储,在早期受到广泛的应用.而后随着xml使用的广泛,其位置渐渐被取代,不过 ...
- java文件操作(普通文件以及配置文件的读写操作)
转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...
- 操作配置文件Properties
// */ // ]]> 操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...
随机推荐
- Hibernate处理MySQL的时间
如果Java使用使用 java.util.Date 作为持久化对象属性,在 Mysql 中使用 YEAR.DATE.TIME.TIMESTAMP.DATETIME 五种类型 则需要添加@Tempora ...
- 06.python语法入门--与用户交互、运算符
与用户交互 输入 input # python2与python3的区别 '''python3''' # 将获取到的用户输入赋值给变量名res res = input(' ...
- Redis入门与实践(附项目真实案例代码)
我是3y,一年CRUD经验用十年的markdown程序员常年被誉为优质八股文选手 今天继续更新austin项目,如果还没看过该系列的同学可以点开我的历史文章回顾下,在看的过程中不要忘记了点赞哟!建议 ...
- Java的泛型机制
Java的泛型机制 泛型是 Java 从 JDK5 开始引入的新特性,本质上是参数化类型,即所操作的数据类型被指定为一个参数.这意味着编写的代码可以被很多不同类型的对象所重用. 1. 泛型的使用方式 ...
- OpenStack、虚拟机以及和当前流行的k8s、Docker四者之间的关系
一.OpenStack与虚拟机之间的关系 OpenStack使用Python语言开发,是虚拟资源管理工具,他可以协助你搜集各种资源,并加以利用以及管理,实现物理资源的高效使用和安全.虚拟化物理机这个动 ...
- ios开发 Pods工具心得
Pods 这也是我的第一篇微博,希望能给大家带来帮助,也便于我自己温习 第一步:新建一个xcode项目(这个不解释了) 第二步:打开终端(剩下的操作都在终端里面了)
- python中面向对象知识框架
案列: 1 class Chinese: # 类的创建,类名首字母要大写 2 eye = 'black' # 类属性的创建 3 4 def __init__(self,hometown): # 类的初 ...
- k8s基础环境配置:基于CentOS7.9
k8s基础环境配置:基于CentOS7.9 wmware15安装centos7.9:https://www.cnblogs.com/uncleyong/p/15261742.html 1.配置静态ip ...
- uniapp vue3 $on/$once/$off 的替代方案
仅作参考 仅作参考 仅作参考 并且只支持页面生命周期使用 不支持组件 不支持页面函数方法 下面说了思路需要的话自己添加 今天用到 $once 时发现报错了,原理是vue3移除了该api.我一开始想的是 ...
- Google发布跨云Serverless管理平台Knative
企业只要使用由Google与Pivotal.IBM.红帽和SAP等企业共同开发的跨云Serverless管理平台Knative,就能在支持Kubernetes的云平台上自由的迁移工作负载,无论是跨私有 ...