BufferedReader in=new BufferedReader(new FileReader("源文件的路径");
bufferedWriter out=new BufferedWriter(new FileWriter("目标文件路径");
int c;
while((c=in.read())!=-1)
 out.write(c);
in.close;
out.close;

解决方案 »

  1.   

    public  static  void  copyfile(String  path1,String  path2)  throws  IOException
            {
              FileInputStream fi = new FileInputStream(path1);
              FileOutputStream fo = new FileOutputStream(path2);
              byte date[] = new byte[fi.available()];
              fi.read(date);
              fo.write(date);
              fi.close();
              fo.close();
            }
      

  2.   

    to CyberH() :
        一个一字节的读取实在是速度很慢,如果文件比较大的话,很有可能由于IO阻塞而令程序停止反应,我曾经试过以这种方式从一个压缩包中读取文件,那速度实在是太慢了。
        可以定义一个缓冲区,每次从文件读取一定的数据,写到目标文件。    /**
        *
        */
        void copy(String src,String des) throws IOException{        int bufsize=100;//100Byte
            File srcFile=new File(src);
            File desFile=new File(des);
            byte[] buf=new byte[bufsize];//作为缓冲,大小为100Byte
            FileInputStream in=new FileInputStream(srcFile);
            int leave=in.available();
            while(leave!=-1){
                if(leave>bufsize){
                    in.read(buf,0,bufsize);
                }
                else{
                    in.read(buf,0,leave);
                }
                leave=in.available();
            }
        }    不过控制比较麻烦(当剩下的数据没有缓冲区大,你必需知道并准确从缓冲区读出来),但是比较好用,我一般这样使用。
      

  3.   

    漏了写文件 :)
    将缓冲区的数据写到FileOutputStream就是了。
    当定义的缓冲区大小是文件的大小,就和 pqds(韩富贵)  说的一样了。
      

  4.   

    hehecafe(呵呵咖啡馆)的方法可改進一下﹐因為剩下的數據會丟失.
        void copy(String src,String des) throws IOException{
          FileInputStream from;
          FileOutputStream to;
          try{  
            int bufsize=4096;//一個TCP/IP包可能有4k
            int buffer_reads;
            File srcFile=new File(src);
            File desFile=new File(des);
            byte[] buf=new byte[bufsize];
            from =new FileInputStream(srcFile);
            to = new FileOutputStream(desFile);
            int leave=in.available();
            while((bytes_reads = from.read(buf))!= -1){
              to.write(buf,0,bytes_reads);
            }
          }finally{
            if (from != null) try{ from.close();} catch(IOException e){}
            if (to != null) try{ to.close();} catch(IOException e){}
          }
        }這只是一個大致和程序﹐我沒運行過﹐你可能還要改一下﹐另外加些功能可惜﹐又沒分
      

  5.   

    沒注意﹐下面這句要去掉
    int leave=in.available();
      

  6.   

    欢迎您:iceyou 可用分:0 总信誉分:100 注销我的登录