之前項目中用到的代碼塊(讀取配置文件會亂碼):
public class PRopertiesConfig { private static Logger log = Logger.getLogger(PropertiesConfig.class); public PropertiesConfig() { } private static Properties props = new Properties(); static { try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("propertyconfig.properties")); } catch (FileNotFoundException e) { log.error(e); } catch (IOException e) { log.error(e); } } public static String getValue(String key) { return props.getProperty(key); }}但是當配置文件中存在如下類型的key=value時,會出現(xiàn)value亂碼
user_name=王大力此時讀取到的“王大力“亂碼,解決方法(更改靜態(tài)代碼塊):
static { try { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("msgconfig.properties"); BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); props.load(bf); } catch (FileNotFoundException e) { log.error(e); } catch (IOException e) { log.error(e); } }這樣讀到的值不會亂碼。
新聞熱點
疑難解答