一、 對(duì)于.properties文件的讀取容易出現(xiàn)的中文讀取亂碼問題
1、原始代碼 未加讀取.properties文件的規(guī)則
public class CityCodeUtils {
private static Properties properties = new Properties();
public static String DEFAULT_CODE = "101010100";
private CityCodeUtils() {
}
static {
ClassLoader classLoader = CityCodeUtils.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("文件名.properties");
try {
properties.load(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getCityCode(String cityName) {
String code = properties.getProperty(cityName, "101010100");
return code;
}
}
debug運(yùn)行截圖:
2、加入.properties讀取規(guī)則文章來源:http://www.zghlxwxcb.cn/news/detail-567241.html
public class CityCodeUtils {
private static Properties properties = new Properties();
public static String DEFAULT_CODE = "101010100";
private CityCodeUtils() {
}
static {
ClassLoader classLoader = CityCodeUtils.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("文件名.properties");
try {
//解決讀取properties文件時(shí)的中文亂碼問題
//給中文讀取設(shè)置為utf-8編碼規(guī)則 若不加BufferedReader 設(shè)置的編碼規(guī)則
//則很有可能在讀取文件名.properties 時(shí)出現(xiàn)亂碼
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"utf-8"));
properties.load(bufferedReader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
debug截圖:
至此 問題已解決!文章來源地址http://www.zghlxwxcb.cn/news/detail-567241.html
到了這里,關(guān)于解決.properties文件中文讀取亂碼問題(idea Java)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!