java读取全局properties属性文件并解决中文乱码问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class Config {

private static Logger logger = Logger.getLogger(Config.class);

public class Key {
public static final String SmsPattern = "sms.pattern";
}

public Config(){}
private static Properties props = new Properties();
static{
try {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("../config.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
props.load(bf);
logger.info("config load success.");
} catch (IOException e) {
logger.fatal(e);
}
}
public static String getValue(String key){
return props.getProperty(key);
}
}