问题:一个文档是GBK编码,写了一大串中文;现在把它转成UTF-8的编码
  以下代码是别人可以实现的代码,但是有点疑惑.
==============================================================================================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class Test {
    public static void main(String[] args) throws IOException {
        File file=new File("d:"+File.separator+"a.txt");
        File outFile=new File("d:"+File.separator+"b.txt");
        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
        String line=br.readLine();
        while (line!=null) {
            bw.write(line);
            System.out.println(line);    
            line=br.readLine();
        }
        bw.flush();
        bw.close();
        br.close();
    }
}
============================================================================
 下次是我的几点疑惑:
1   对于一个中文字而言,gbk编码是怎么转化为UTF-8的。我的理解是 首先gbk编码首先在readLine()转化为Unicode,在写入时,Unicode再转化为UTF-8;但是这转化过程中可以发现  gbk转化Unicode是没有问题的,中字都是占2个字节,但是在Unicode转UTF-8时,2个字节的中文要转为3个字节存储,这时不会出现问题吗?还是说UTF-8在转化过程中,虽然存储字节改变,但是信息依然不变,显示在txt文档中的字依然可以显示?
2   new String(s.getBytes("gbk"),"UTF-8"); 个人觉得String的构造方法和 上边实现的功能差不多,都是gbk编码向UTF转化,但是为什么是错的呢    希望各位能详细的解答一下

解决方案 »

  1.   

     IOstream 默认的就是gbk吧,我认为在得到BufferedReader的实例的时候,不需要在转了吧。
      我认为不管怎么转 都没问题,因为本质不会变; 传播的时候是ISO8859-1吧
      
      

  2.   

    传播的时候是ISO8859-1?这个有证明吗···
      

  3.   

    不是太懂
    给你个网址,编码是很复杂的东西
    http://www.cnblogs.com/NanguoCoffee/articles/1886323.html
      

  4.   

    http://topic.csdn.net/u/20110623/17/436c44a9-5988-4aba-a2d0-94ebef4c5080.html
    希望对你有帮助,汉字编码是个很复杂的东西