有些文件中存在Unicode字符和非Unicode字符,如何利用java快速的把文件中的Unicode字符轉換為漢字而不影響文件中的其他字符呢,
我們知道雖然java 在控制台會把Unicode字符直接輸出成漢字,但是當遇到文件中的Unicode和非Unicode字符在一起的時候卻不好用了。
下面是代碼,只需要把代碼中的路徑替換為你想要的路徑,在建立一個轉換后的文件路徑。其他代碼無需改變。
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 9 10 public class Zhtest { 11 12 public static void main(String[] args) throws IOException { 13 //源文件路徑 14 String path = "d:\\Blaze.txt"; 15 //輸出文件路徑 16 File write = new File("d:\\Blaze1.txt"); 17 18 File file = null; 19 BufferedReader br = null; 20 BufferedWriter bw = new BufferedWriter(new FileWriter(write)); 21 file = new File(path); 22 br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk")); 23 StringBuilder sb = new StringBuilder(); 24 String length = ""; 25 while ((length = br.readLine()) != null) { 26 sb.append(length); 27 bw.write(ascii2Native(sb.toString()) + "\r\n"); 28 bw.flush(); 29 sb = new StringBuilder(); 30 } 31 32 } 33 34 public static String ascii2Native(String str) { 35 StringBuilder sb = new StringBuilder(); 36 int begin = 0; 37 int index = str.indexOf("\\u"); 38 while (index != -1) { 39 sb.append(str.substring(begin, index)); 40 sb.append(ascii2Char(str.substring(index, index + 6))); 41 begin = index + 6; 42 index = str.indexOf("\\u", begin); 43 } 44 sb.append(str.substring(begin)); 45 return sb.toString(); 46 } 47 48 private static char ascii2Char(String str) { 49 if (str.length() != 6) { 50 throw new IllegalArgumentException("長度不足6位"); 51 } 52 if (!"\\u".equals(str.substring(0, 2))) { 53 throw new IllegalArgumentException("字符必須以 \"\\u\"開頭."); 54 } 55 String tmp = str.substring(2, 4); 56 int code = Integer.parseInt(tmp, 16) << 8; 57 tmp = str.substring(4, 6); 58 code += Integer.parseInt(tmp, 16); 59 return (char) code; 60 } 61 62 }