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工具类的更多相关文章

  1. 【转载】ASP.NET工具类:文件夹目录Directory操作工具类

    在ASP.NET开发网站的过程中,有时候会涉及到文件夹相关操作,如判断文件夹目录是否存在.删除文件夹目录.创建文件.删除文件.复制文件夹等等.这一批有关文件目录的操作可以通过Directory类.Fi ...

  2. 文件处理工具 gif合成工具 文件后缀批量添加工具 文件夹搜索工具 重复文件查找工具 网页图片解析下载工具等

    以下都是一些简单的免费分享的工具,技术支持群:592132877,提供定制化服务开发. Gif动图合成工具 主要功能是扫描指定的文件夹里的所有zip文件,然后提取Zip文件中的图片,并合成一张gif图 ...

  3. 使用MAT(Memory Analyzer Tool)工具分析dump文件--转

    原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...

  4. MyBatis Java不同方式加载文件时的路径格式问题、Mybatis中加载.properties文件

    public class LoadPropTest { public static void main(String[] args) throws IOException { //一.Properti ...

  5. 曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  6. Java程序读取Properties文件

    一.如果将properties文件保存在src目录下 1.读取文件代码如下: /** * 配置文件为settings.properties * YourClassName对应你类的名字 * / pri ...

  7. 用类加载器的5种方式读取.properties文件

    用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...

  8. 161216、使用spring的DefaultResourceLoader自定义properties文件加载工具类

    import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; imp ...

  9. Java读取properties文件工具类并解决控制台中文乱码

    1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...

随机推荐

  1. 【POJ1743】 Musical Theme (二分+后缀数组)

    Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)note ...

  2. MVVM_Android-CleanArchitecture

    前言 "Architecture is About Intent, not Frameworks" - Robert C. Martin (Uncle Bob) Uncle Bob ...

  3. php中header函数后是否应该有exit

    通常有一下三种:  代码如下 复制代码 Location: xxxx:yyyy/zzzz Content-Type: xxxx/yyyy Status: nnn xxxxxx 常用实例 1.实现重定向 ...

  4. 通过 WIN32 API 实现嵌入程序窗体

    写了一个不使用 COM, 而是通过 WIN32 API 实现的示例, 它把写字板程序嵌在了自己的一个面板中. 这么做可能没有实际意义, 因为两个程序之前没有进行有价值的交互, 这里仅仅是为了演示这么做 ...

  5. BZOJ3156: 防御准备

    3156: 防御准备 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 442  Solved: 210[Submit][Status] Descript ...

  6. HNOI 2008:水平可见直线

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的,否则Li为被覆盖的. 例如,对于直线: L1:y ...

  7. ExtJS练手

    Ext.onReady(function () { //给grid添加数据源 var store = new Ext.data.JsonStore({ root: 'rows', totalPrope ...

  8. devi into python 笔记(七)locals与globals 字典格式化字符串 字符集

    locals()与globals(): """ locals:局部命名空间 globals:全局命名空间 都是以dictionary的形式保存的,变量名是键,变量值是值 ...

  9. 对使命召唤OL游戏中队友能相互救治的动作设定的感慨

    很偶然的在网吧看到有人在玩一个枪战游戏,场景特别真实特别吸引人,后来留意到是使命召唤OL.我使用QQ帐号(是腾讯代理)玩了一次,觉得游戏做的确实精致,子弹打击效果和人物被子弹击中的效果特别真实,大家可 ...

  10. Bzoj 2346: [Baltic 2011]Lamp dijkstra,堆

    2346: [Baltic 2011]Lamp Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 428  Solved: 179[Submit][Sta ...