如何利用java把文件中的Unicode字符转换为汉字

时间:2022-05-25 08:42:05

有些文件中存在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 }