读取中文文件名时发现有些GB18030编码的字符不能正确转换成UTF8编码(不是扩展字符)。
比如:String为"测试文件(0.3版)"(两个括号均为全角0.3为半角) 在使用 String str=new String(s.getBytes("GB18030"),"UTF8") 转换后为"测试文件$1?73版"。不知道那位高手教我能正确转换这种字符串的办法?

解决方案 »

  1.   

    你用GB18030编码得到byte[], 再用UTF-8去解码编码格式为GB18030的byte[],当然乱码。
    String str1=new String(str.getBytes("GB18030"),"GB18030");
      

  2.   

    我试了一下,直接输出不行,但如果是文件要编码的话可以这样,输出的test2.txt就是utf8编码的,test1.txt是gb编码的文本文件
        File f=new File("F:\\test\\test.txt");
        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f),"GB18030"));
        File f2=new File("F:\\test\\test2.txt");
        if(!f2.exists()){
          f2.createNewFile();
        }
        PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(f2),"UTF8"));
        String str=null;
        while((str=br.readLine())!=null){
          pw.write(str+"\n");
        }
        br.close();
        pw.close();
      

  3.   

    天哪,难道取个文件名也要......
    不过没别的办法也只好如此了。
    谢谢zh__ua的回答
    也感谢xstom19的捧场