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. 拔掉网线后, 原本的 TCP 连接还存在吗?

    大家好,我是小林. 今天,聊一个有趣的问题:拔掉网线几秒,再插回去,原本的 TCP 连接还存在吗? 可能有的同学会说,网线都被拔掉了,那说明物理层被断开了,那在上层的传输层理应也会断开,所以原本的 T ...

  2. Oracle用户创建、删除和授权等方法总结

    一.查看用户及权限 1.查询所有用户: 1.1.查看所有用户基本信息 select * from all_users; 1.2.查看所有用户相信信息 select * from dba_users; ...

  3. 这个杀手不太冷-kill家族

    文章目录 kill killall pkill 跑路小技巧 kill家族: kill: # 删除执行中的程序或工作 killall: # 使用进程的名称来杀死进程,使用此指令可以杀死一组同名进程 pk ...

  4. 使用Flask开发简单接口

    作为测试人员,在工作或者学习的过程中,有时会没有可以调用的现成的接口,导致我们的代码没法调试跑通的情况. 这时,我们使用python中的web框架Flask就可以很方便的编写简单的接口,用于调用或调试 ...

  5. [Java]Java入门笔记(一):IDE设置、部分快捷键

    一.Eclipse 软件设置 注意 同一时间,工作空间只能使用1个. 1.1 创建程序的步骤 创建项目Java Project 注意:项目名不要使用数字,也不要以数字开头: 选择"Use d ...

  6. PentestBOX教程

    0x01 Pentest BOX Pentest Box:渗透测试盒子,是一款Windows平台下预配置的便携式开源渗透测试环境,而它也是著名黑客Kapustkiy常用的工具之一.这里集成的大都是Li ...

  7. 想找好用的BI软件?看这一篇就够了:2021年好用的BI软件推荐

    很多厂商活跃在商业智能(下面称BI)领域.事实上,能够满足用户需要的BI产品和方案必须建立在稳定.整合的平台之上,该平台需要提供用户管理.安全性控制.连接数据源以及访问.分析和共享信息的功能.那么,有 ...

  8. 【C# .Net GC】GC的类型与工作方式 和配置

    .net主要有两种类型垃圾回收器,也可也说是垃圾回收器的两种工作模式. GC的类型主要有两种: 工作模式是针对进程的,程序启动后就不能修改了.只能在配置文件.json .xml进行设置.但是可用通过G ...

  9. python学习笔记:1、读取文本文件,按行处理

    需求源于 整理 时序报告.按照以前的思路 都是按行行的 进行处理 提取需要的信息,判断. 首先的操作应该是读取. python的读取 两个方法 (1) 通过readline()来进行读取 f = op ...

  10. csv 转换为DBF文件的方法

    转至:https://www.cnblogs.com/hssbsw/archive/2012/12/01/2797140.html csv 转换为DBF文件的方法 最近从SQL导出了许多CSV文件发到 ...