一个读取配置文件的工具类

package com.lw.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
 
/**
 * 获得配置文件的属性值
 * @author create by lw 
 *
 */
public class GetPropertyValue {
	
	//获取日志器对象
	public static Logger log=Logger.getLogger(GetPropertyValue.class);
	
	static Properties prop = null;
	public  static Properties init(String path){
		try {
			prop = new Properties();
			InputStream in= new BufferedInputStream(new FileInputStream(path));
            prop.load(new InputStreamReader(in, "utf-8"));//utf-8格式复制中文乱码
			return prop;
		} catch (Exception e) {
			String msg="class[GetPropertyValue] method[init(String path)] read txt having missing a mistake";
			log.info(msg);
			return null;
		}
	}
	
	//获得配置文件中所有的key值
	public static List GetAllKey(String path) {
		Properties prop =init(path);
		Object[] objs = prop.keySet().toArray();
		List allKey=new ArrayList<>();
		for(int i=0;i<objs.length;i++){
		allKey.add(objs[i]); 
		}
		return allKey;
	}	
	
	//获得配置文件中所有的value值
	public static List GetAllValue(String path) {
		Properties prop =init(path);
		Object[] objs = prop.keySet().toArray();
		List allValue=new ArrayList<>();
		for(int i=0;i<objs.length;i++){
			allValue.add(prop.get(objs[i])); 
		}
		return allValue;
	}
	
	//获得配置文件中所有的key-value值
	public static Map GetAllKV(String path) {
		Properties prop =init(path);
		Object[] objs = prop.keySet().toArray();
		Map KVMap=new HashMap<>();
		for(int i=0;i<objs.length;i++){
			String key=String.valueOf(objs[i]);
			String value=String.valueOf(prop.get(objs[i]));
			KVMap.put(key, value);
		}
		return KVMap;
	}
	
	
	//获得配置文件某个key的value值
	public static Object getOneValueByKey(String path,String key){
		Properties prop =init(path);
		Object[] objs = prop.keySet().toArray();
			for(int i=0;i<objs.length;i++){
				String txtKey=(String) (objs[i]);
				if (key.equals(txtKey)) {
					return prop.get(objs[i]);
				}
			}
			return null;
	}
	
	//将value为逗号分隔的值输出为List(例如f=a,b,c,d,e,恩恩)
	public static List<String>  getValueListByKey(String path,String key){
		List<String> valueList=new ArrayList<>();
		Properties prop =init(path);
		Object[] objs = prop.keySet().toArray();
			for(int i=0;i<objs.length;i++){
				String txtKey=(String) (objs[i]);
				if (key.equals(txtKey)) {
					String valueStr=(String) prop.get(objs[i]);
					String[] split = valueStr.split(",",-1);
					valueList= Arrays.asList(split);
					return valueList;
				}
			}
			return null;
	}
	
	
	//测试
	public static void main(String[] args) {
		//此处为本机路径
		 String path = "D:/lw/property/property.txt";
		 System.out.println(GetAllKey(path));
		 System.out.println(GetAllValue(path));
		 System.out.println(GetAllKV(path));
		 readProperty(path);
		 System.out.println("------------------------------------");
		 System.out.println(getOneValueByKey(path,"f"));
		 System.out.println("------------------------------------");
		 System.out.println(getValueListByKey(path,"f"));
		 System.out.println("------------------------------------");
		 System.out.println(getValueListByKey(path,"addrRep"));
		 List<String> valueListByKey = GetPropertyValue.getValueListByKey(path, "addrRep");
			for (String str : valueListByKey) {
					String[] repArr = str.split("@",-1);
					String addrChange=null;
					try {
						System.out.println(repArr[0]+"=="+repArr[1]);
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
	}		

}

/********测试数据*************************************************/
/*
a=123
b=456
c=789
d=ad
e=sdfsdf爽肤水
f=a,b,c,d,e,恩恩
addrRep=临号@号,后门号@号,边门号@号,甲号@号,乙号@号,丙号@号,丁号@号,乙临号@号
*/



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值