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=临号@号,后门号@号,边门号@号,甲号@号,乙号@号,丙号@号,丁号@号,乙临号@号
*/