|
|
package com.base;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
import com.base.Constant;
|
|
|
|
|
|
public class Resource {
|
|
|
|
|
|
private static Map<String, String> properties = new HashMap<>();
|
|
|
|
|
|
static{
|
|
|
// 读取 properties文件
|
|
|
// readPropertiesFile(Constant.SYSTEM_PROPERTIES_FIEL_PATH);
|
|
|
readPropertiesFile4Chinese(Constant.I18N_PROPERTIES_FIEL_PATH);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @功能 读取配置(解决中文乱码)
|
|
|
* @param filename "/com/utils/exception/i18n.properties" 的格式
|
|
|
*/
|
|
|
public static void readPropertiesFile4Chinese(String filename) {
|
|
|
Properties pro = new Properties();
|
|
|
try {
|
|
|
// 读取属性文件 XXXX.properties(Reader。writer解决中文乱码)
|
|
|
// InputStreamReader in= new InputStreamReader(Resource.class.getClassLoader().getResourceAsStream(filename), "UTF-8");
|
|
|
InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(filename), "UTF-8");
|
|
|
BufferedReader bf = new BufferedReader(in);
|
|
|
// InputStreamReader in = new BufferedInputStream(new FileInputStream(filename));
|
|
|
pro.load(bf); // /加载属性列表
|
|
|
Iterator<String> it = pro.stringPropertyNames().iterator();
|
|
|
while (it.hasNext()) {
|
|
|
String key = it.next();
|
|
|
properties.put(key, pro.getProperty(key));
|
|
|
}
|
|
|
in.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @功能 获得properties
|
|
|
* @return properties
|
|
|
*/
|
|
|
public static Map<String, String> getProperties() {
|
|
|
return properties;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @功能 读取配置
|
|
|
* @param filename 文件绝对路径
|
|
|
*/
|
|
|
public static void readPropertiesFile(String filename) {
|
|
|
Properties pro = new Properties();
|
|
|
try {
|
|
|
// 读取属性文件 XXXX.properties( 中文会 乱码)
|
|
|
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(filename));
|
|
|
pro.load(bf); // /加载属性列表
|
|
|
Iterator<String> it = pro.stringPropertyNames().iterator();
|
|
|
while (it.hasNext()) {
|
|
|
String key = it.next();
|
|
|
properties.put(key, pro.getProperty(key));
|
|
|
}
|
|
|
bf.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|