用同样一段代码,一行一行读入同一个文本文件,处理后又写入新文件中。
在不同的电脑上运行结果竟然不一样,我现在用这台电脑汉字显示正确,但是其他的电脑汉字显示为乱码,还有就是<?这两个字符组合在一起时也显示为乱码,请大家分析一下原因。

解决方案 »

  1.   

    主要代码如下:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package helloworld;import java.io.*;/**
     *
     * @author Administrator
     */
    public class ReadWriteStr_de {
    public static final String UTF_8 = "UTF-8";      /**
          * 字符串编码转换的实现方法
          * @param str    待转换的字符串
          * @param newCharset    目标编码
          */
          public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
              if(str != null) {
                 //用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312
                 byte[] bs = str.getBytes();
                 return new String(bs, newCharset);    //用新的字符编码生成字符串
             }
             return null;
         }      /**
          * 字符串编码转换的实现方法
          * @param str    待转换的字符串
          * @param oldCharset    源字符集
          * @param newCharset    目标字符集
          */
          public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
              if(str != null) {
                 //用源字符编码解码字符串
                 byte[] bs = str.getBytes(oldCharset);
                 return new String(bs, newCharset);
             }
             return null;
         }
         public static String toUTF_8(String str) throws UnsupportedEncodingException {
             return ReadWriteStr_de.changeCharset(str, UTF_8);
         }
    public static void main(String[] args) throws Exception{
            String filePath = "d:/javatest/temp/200910_de_其它支行汇总.xml";
            String tmpFile = "d:/javatest/temp/200910_de_其它支行汇总_new.xml";
            FileReader myFileR=new FileReader(filePath);
            BufferedReader myBufferedReader=new BufferedReader(myFileR);        FileWriter writer=new FileWriter(tmpFile);
            BufferedWriter myBufferedWriter=new BufferedWriter(writer);        String[] linestr=new String[100000];
            int i=0;        //将文件读入内存
            while((linestr[i]=toUTF_8(myBufferedReader.readLine()))!=null){
                i++;
            }
            i--;        //省去对linestr处理的过程若干
            for(int m=0;m<i;m++){
                //System.out.println(linestr[m]);
                myBufferedWriter.write(linestr[m]+"\n");
            }
            
            myBufferedWriter.flush();
            myBufferedWriter.close();
            myBufferedReader.close();
            myFileR.close();
            writer.close();
            
        }
    }
      

  2.   

    跟你系统的或者是ide工具中字符集不一致导致的,你都设置为一致的就能解决了
      

  3.   


                 //用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312
                 byte[] bs = str.getBytes();如果你读的文件是gb2312 或者gbk编码的,可以把byte[] bs = str.getBytes("GBK");写死。如果别人的操作系统不是中文windows ,那么他的默认编码就不是gbk,用默认编码去getBytes就会乱码。你可以加个System.out.println(System.getProperty("sun.jnu.encoding"));