public static void copyFile(File src,File dest){
        try{
            FileInputStream in = new FileInputStream(src);
            FileOutputStream out = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length = -1;
            while((length = in.read(buffer)) != -1){
                out.write(buffer,0,length);
            }
            out.flush();
            out.close();
            in.close();
        }catch(Exception e){
            System.out.println(e);
        }
    }
上面的是拷贝文件的方法,我的XML文件格式是UTF-8,里面有一堆中文,经过上面的方法拷贝到另一个地方后,里面的中文就变成了乱码,我该如何解决呢?
另:我的XML例子
<?xml version="1.0" encoding="UTF-8"?>
<AA>
  <BB id="1" name="中文">
    <CC name="中文"/>
  </BB>
  <BB id="2" name="中文"/>
</AA>

解决方案 »

  1.   

    我是用EditPlus打开的,关键是如何拷贝没问题(XML文件不出现乱码),我的程序就没问题,若出现乱码,我的程序就因乱码而出错.有些时候拷贝就出现乱码,不是所有拷贝都是乱码,怎么能控制都不是乱码呢?
      

  2.   

    编辑器的格式在IDE中一定要设置好,
      

  3.   

    InputStreamReader is = new InputStreamReader(new FileInputStream(new File("fileName")),"UTF-8");
    用"utf-8"编码输入流看看
      

  4.   

    为啥不用系统调用呢?
    Windows的话
    Runtime.getRuntime.exec("cmd /c copy oldfile newfile");
    UNIX的话
    Runtime.getRuntime.exec("cp oldfile newfile");
      

  5.   

    楼主回复:拷贝后的XML文件和原来的大小不一样.编辑器的格式在IDE中一定要设置好
    (怎么设置???)
      

  6.   

    楼主回复:windvscloud(大翼) 你好!InputStreamReader is = new InputStreamReader(new FileInputStream(new File("fileName")),"UTF-8");怎么改写我上面的代码,能贴出来么,谢谢
      

  7.   

    拷贝后的XML文件和原来的大小不一样,那就是拷贝的时候有问题。
    不过看这段代码是没有问题的,奇怪。
      

  8.   

    用字符流FileReader和FileWriter试试