• 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/

  • 相关阅读:
    v-bind 和v-model 的区别
    解决PC端和移动端自适应问题?
    安全解决将字符串" "转换成换行
    最全的正则表达式-匹配中英文、字母和数字(转)
    vue:style标签中的scoped属性(作用域)和lang属性的介绍
    vue项目main.js文件下import router from './router'默认导入router文件夹下index.js的原因
    VUE修改样式无效
    lodop如何获取打印机名称
    深拷贝和浅拷贝
    Vue.js学习笔记:props传递数据(转)
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227107.html
Copyright © 2020-2023  润新知