方便对properties文件读取的工具类
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* <p>
* PropertiesFileReadUtil.java
* properties文件读取工具
* </p>
* @since 2019年2月28日 上午8:56:09
* @author XinLau
* @version 1.0
*/
public class PropertiesFileReadUtil {
/**
* <p>
* getPropertiesMap
* 获取properties文件
* </p>
* @param filePath
* @return
* @return Map<String,String>
* @author XinLau
* @since 2019年2月28日上午9:32:31
*/
public static Map<String,String> getPropertiesMap(String filePath){
Properties prop = new Properties();
try {
// 通过输入缓冲流读取properties文件
InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(inputStream);
return PropertiesMap(prop);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* <p>
* getPropertiesKeyList
* 获取properties文件的Key
* </p>
* @param filePath
* @return List<String>
* @author XinLau
* @since 2019年2月28日上午9:35:33
*/
public static List<String> getPropertiesKeyList(String filePath){
Properties prop = new Properties();
try {
// 通过输入缓冲流读取properties文件
InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(inputStream);
return KeyList(prop);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* <p>
* getPropertiesValueList
* 获取properties文件的Value
* </p>
* @param filePath
* @return List<String>
* @author XinLau
* @since 2019年2月28日上午9:36:05
*/
public static List<String> getPropertiesValueList(String filePath){
Properties prop = new Properties();
try {
// 通过输入缓冲流读取properties文件
InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(inputStream);
return ValueList(prop);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* <p>
* PropertiesFileRead
* 返回properties文件的MAP集合
* </p>
* @param props properties文件
* @return Map<String,String>
* @author XinLau
* @since 2019年2月28日上午9:12:55
*/
private static Map <String,String> PropertiesMap(Properties props){
Map <String,String> map = new HashMap<>();
Enumeration<?> en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = props.getProperty(key);
map.put(key, value);
}
return map;
}
/**
* <p>
* KeyList
* 返回properties文件的Key数组
* </p>
* @param props properties文件
* @return List<String>
* @author XinLau
* @since 2019年2月28日上午9:18:19
*/
private static List<String> KeyList(Properties props){
List <String> keyList = new ArrayList<String>();
Enumeration<?> en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
keyList.add(key);
}
return keyList;
}
/**
* <p>
* KeyList
* 返回properties文件的Value数组
* </p>
* @param props properties文件
* @return List<String>
* @author XinLau
* @since 2019年2月28日上午9:18:19
*/
private static List<String> ValueList(Properties props){
List <String> valueList = new ArrayList<String>();
Enumeration<?> en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String value = props.getProperty(key);
valueList.add(value);
}
return valueList;
}
public static void main(String[] args) {
String PROPERTIESFILEPATH = "/imis-config/src/main/resources/jdbc.properties";
Map<String,String> A = PropertiesFileReadUtil.getPropertiesMap(PROPERTIESFILEPATH);
List <String> keyList = PropertiesFileReadUtil.getPropertiesKeyList(PROPERTIESFILEPATH);
List <String> valueList = PropertiesFileReadUtil.getPropertiesValueList(PROPERTIESFILEPATH);
for (String key : keyList) {
String value = A.get(key);
System.out.println(key + ":" + value);
}
for (String value : valueList) {
System.out.println(value);
}
}
}