在java中String aa = "\u4E2D\u6587";
System.out.println(aa)能正常显示“中文”两个字。但是如果把“\u4E2D\u6587”放到txt中,再读取到java当中时,就只能是“\u4E2D\u6587”,不能显示“中文”这两个字了。有人知道为什么吗?怎么才能正常显示呢?我不想用properties文件!!!!!!

解决方案 »

  1.   

    你需要用native2assic转换下,因为txt文档中的是本地化的字符,也就是说是gbk的字符,但是你要放在ide中让他显示“中国”就要转换成assic码,所以要用java自带的转换工具,然后从assic码编译成ide默认的utf-8格式,这个问题属于java国际化的部分
      

  2.   

    //jvm对unicode的处理是在编译时就完成了,现在写到了文件中,编译时是无法进行处理的,只好等到运行时把Unicode转化成本地字符。import java.io.*;
    public class unicodeTest{
        private static String loadConvert (char[] in, int off, int len, char[] convtBuf) {
            if (convtBuf.length < len) {
                int newLen = len * 2;
                if (newLen < 0) {
                newLen = Integer.MAX_VALUE;
            } 
            convtBuf = new char[newLen];
            }
            char aChar;
            char[] out = convtBuf; 
            int outLen = 0;
            int end = off + len;        while (off < end) {
                aChar = in[off++];
                if (aChar == '\\') {
                    aChar = in[off++];   
                    if(aChar == 'u') {
                        // Read the xxxx
                        int value=0;
                for (int i=0; i<4; i++) {
                    aChar = in[off++];  
                    switch (aChar) {
                      case '0': case '1': case '2': case '3': case '4':
                      case '5': case '6': case '7': case '8': case '9':
                         value = (value << 4) + aChar - '0';
                     break;
                  case 'a': case 'b': case 'c':
                              case 'd': case 'e': case 'f':
                     value = (value << 4) + 10 + aChar - 'a';
                     break;
                  case 'A': case 'B': case 'C':
                              case 'D': case 'E': case 'F':
                     value = (value << 4) + 10 + aChar - 'A';
                     break;
                  default:
                                  throw new IllegalArgumentException(
                                               "Malformed \\uxxxx encoding.");
                            }
                         }
                        out[outLen++] = (char)value;
                    } else {
                        if (aChar == 't') aChar = '\t'; 
                        else if (aChar == 'r') aChar = '\r';
                        else if (aChar == 'n') aChar = '\n';
                        else if (aChar == 'f') aChar = '\f'; 
                        out[outLen++] = aChar;
                    }
                } else {
                out[outLen++] = (char)aChar;
                }
            }
            return new String (out, 0, outLen);
        }
        public static void main(String rags[])throws Exception{
             BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
             String line="";
             while((line=br.readLine())!=null){
                 char[] convtBuf=new char[2];
                 System.out.println(loadConvert(line.toCharArray(),0,12,convtBuf));
             }
             br.close();
        }
    }
    1.txt里面的内容是:\u4E2D\u6587