遇到的問題
java讀取文件時,因爲字節碼的不同,有些時候,中文存在文件中是\uxxx的形式,讀取文件時我們肯定要轉換成我們可以看懂的中文,採用以下方法即可。
解決方法
/**
* unicode轉中文
* @param str
* @return
* @author yutao
* @date 2017年1月24日上午10:33:25
*/
public static String unicodeToString(String str) {
Pattern compile = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = compile.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch+"" );
}
return str;
}