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

  • 相关阅读:
    企业微信中如何屏蔽另外一个企业的消息
    国际商标注册里面的头头道道
    如何使用charles,常见的charles功能
    Win8中GridVIew千变万化——绑定分组数据
    Win8中ListBox的ScrollIntoView方法失效
    一起零基础通过Nosql考试~~
    Ubuntu部署ros小龟爬爬实例
    学习算法是件怎样的事
    Centos7配置Python3.10.1
    基于Centos7、PHP7.4环境部署wordpress
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227107.html
Copyright © 2020-2023  润新知