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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值