import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test{
    public static void main(String[] args) throws IOException{
      
        File f = new File("d:\\test.txt");
        File f1 = new File("e:\\test.txt");        FileReader reader = new FileReader(f);
        FileWriter writer = new FileWriter(f1);
        char[] buff = new char[512];
        int len =0;
        while((len=reader.read(buff))!=-1){
           String str =  new String(buff,0,len);
            writer.write(str);
            writer.flush();
        }
        
        reader.close();
        writer.close();
    }
}

解决方案 »

  1.   

    我在aap上不会发博客,枯了
      

  2.   

    我在aap上不会发博客,枯了
      

  3.   

    还是用finally 关闭stream 吧,稳健些
      

  4.   

    这么写不是复制空文件吗   hello java呢
      

  5.   


     public static void main(String[] args) throws Exception {
        String path = "/Users/Desktop/aa";
        File file1 = new File(path, "1.pdf");
        File file2 = new File(path, "2.pdf");
        if (!file1.exists()) {
          System.out.println("文件" + file1 + "不存在");
          return;
        }
        if (file2.exists()) {
          System.out.println("文件已存在,执行删除操作");
          file2.delete();
        }
        System.out.println("开始复制" + file2);
        try (InputStream in = new BufferedInputStream(new FileInputStream(file1));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(file2))) {
          int c = 0;
          int total = 0;
          while (c != -1) {
            c = in.read();
            total += c;
            out.write((char) c);      }
          System.out.println("文件复制完毕共" + total + "字节");
        }
      }