Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年8月9日 11:31:12 星期二
http://fanshuyao.iteye.com/
package com.chinagas.common.utils; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class PropertiesUtils { private static Logger log = LoggerFactory.getLogger(PropertiesUtils.class); /**
* 根据文件名获取Properties对象
* @param fileName
* @return
*/
public static Properties read(String fileName){
InputStream in = null;
try{
Properties prop = new Properties();
//InputStream in = Object.class.getResourceAsStream("/"+fileName);
in = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
prop.load(in);
return prop;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} /**
* 根据文件名和键名获取值
* @param fileName
* @param key
* @return
*/
public static String readKeyValue(String fileName, String key){
Properties prop = read(fileName);
if(prop != null){
return prop.getProperty(key);
}
return null;
} /**
* 根据键名获取值
* @param prop
* @param key
* @return
*/
public static String readKeyValue(Properties prop, String key){
if(prop != null){
return prop.getProperty(key);
}
return null;
} /**
* 写入
* @param fileName
* @param key
* @param value
*/
public static void writeValueByKey(String fileName, String key, String value){
Map<String, String> properties = new HashMap<String, String>();
properties.put(key, value);
writeValues(fileName, properties);
} /**
* 写入
* @param fileName
* @param properties
*/
public static void writeValues(String fileName, Map<String, String> properties){
InputStream in = null;
OutputStream out = null;
try {
in = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(in);
String path = PropertiesUtils.class.getResource("/"+fileName).getPath();
out = new FileOutputStream(path);
if(properties != null){
Set<String> set = properties.keySet();
for (String string : set) {
prop.setProperty(string, properties.get(string));
log.info("更新"+fileName+"的键("+string+")值为:"+properties.get(string));
}
}
prop.store(out, "update properties");
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(in != null){
in.close();
}
if(out != null){
out.flush();
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
} public static void main(String[] args) throws Exception {
//System.out.println("read="+read("config.properties")); //System.out.println("readKeyValue="+readKeyValue("config.properties","superAdmin")); //writeValueByKey(CC.WEIXI_PROPERTIES, "access_token", "ddd"); Map<String, String> properties = new HashMap<String, String>();
properties.put("access_token", "ddd2");
properties.put("access_token1", "ee2");
properties.put("bbbb", "bbbb");
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年8月9日 11:31:12 星期二
http://fanshuyao.iteye.com/
Property工具类,Properties文件工具类,PropertiesUtils工具类的更多相关文章
- 【转载】ASP.NET工具类:文件夹目录Directory操作工具类
在ASP.NET开发网站的过程中,有时候会涉及到文件夹相关操作,如判断文件夹目录是否存在.删除文件夹目录.创建文件.删除文件.复制文件夹等等.这一批有关文件目录的操作可以通过Directory类.Fi ...
- 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等
以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...
- 使用MAT(Memory Analyzer Tool)工具分析dump文件--转
原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...
- MyBatis Java不同方式加载文件时的路径格式问题、Mybatis中加载.properties文件
public class LoadPropTest { public static void main(String[] args) throws IOException { //一.Properti ...
- 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Java程序读取Properties文件
一.如果将properties文件保存在src目录下 1.读取文件代码如下: /** * 配置文件为settings.properties * YourClassName对应你类的名字 * / pri ...
- 用类加载器的5种方式读取.properties文件
用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...
- 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...
- Java读取properties文件工具类并解决控制台中文乱码
1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...
随机推荐
- [转贴]关于C++的抽象的一点新认识
http://my.oschina.net/fzyz999/blog/138491 关于本文 本文是笔者在阅读<C++沉思录>第0章——序幕后的一点想法,可以算作是笔记也可以算作是读后感. ...
- Visual Studio 2015 Update 1 成功安装后运行 “出现未能正确加载[XXXX]包,此问题可能是由配置更改或安装另一个扩展导致的。” 可能的解决方法
作死装Visual Studio 2015 update 1.安装过程中虽然波澜不惊,但是安装之后运行回报未能正确安装[XXXX]包.找了半天,在stackoverflow中找到了相关的问题,在问题描 ...
- 集合ArrayList习题练一练——分数
namespace 集合习题练一练{ class Program { static void Main(string[] args) { ...
- bzoj1202
很久以前写的,忘补解题报告了首先似乎dfs就可以了吧?但还有更高大上的做法其实这东西就是告诉sum[y]-sum[x-1]=z然后给出一堆看成不成立可以用并查集,维护每个点到father点的差即可 . ...
- [置顶]VC2013的一个bug
[置顶]VC2013的一个bug 前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其 ...
- ARM学习笔记10——GNU ARM命令行工具
一.编译器arm-linux-gcc 1.用arm-linux-gcc编译一个程序,一般它是要经过如下步骤的: 1.1.预处理阶段 编译器把上述代码中stdio.h编译进来,使用GCC的选项-E可以使 ...
- ShadowGun Deadzone 放出 GM Kit Mod 包
一向在技术上比较开放的 MadFinger 继上次给出 shadowgun 的关卡包之后,这次更加大方的给出了更加完整的关卡的代码,甚至包括服务器:ShadowGun Deadzone GM Kit. ...
- [SAN4N学习笔记]使用SysTick精准延时
一.准备工作: 将上一节搭建的LED工程复制一份,命名为"2.systick".这一节主要讲如何使用系统的SysTick节拍定时器来进行精准延时程序. 二.程序编写: S ...
- Tornado源码探寻(准备阶段)
上一篇从一个简单的例子大致了解到Tornado框架的一个概述,同时也看清了web框架的本质. 接下来,我们从tornado程序的起始来分析其源码: 一.概述 上图是摘自朋友的博客里的内容,这张图很明确 ...
- 使用GruntJS链接与压缩多个JavaScript文件
使用GruntJS链接与压缩多个JavaScript文件 自己写了个简单的HTML5 Canvas的图表库,可以支持饼图,折线图,散点图,盒子图 柱状图,同时支持鼠标提示,绘制过程动画效果等.最终我想 ...