服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

拷贝文件的源代码

  //-----------class copyfile begin---------
  //class copyfile
  //copyright:writed by yun-fengsheng 
  //last modify time:2001-11-16
  //method: public boolean copy(string from_filename,string to_filename)

  class copyfile
  {
   public boolean copy(string file1,string file2)
   {
    try //must try and catch,otherwide will compile error
       {
    //instance the file as file_in and file_out
    java.io.file file_in=new java.io.file(file1);
    java.io.file file_out=new java.io.file(file2);
    fileinputstream in1=new fileinputstream(file_in);
          fileoutputstream out1=new fileoutputstream(file_out);
    byte[] bytes=new byte[1024];
    int c;
    while((c=in1.read(bytes))!=-1)
     out1.write(bytes,0,c);
    in1.close();
    out1.close();
    return(true);  //if success then return true
          }

      catch(exception e)
         {  
      system.out.println("error!");
      return(false);  //if fail then return false
         }
   }
  }

  //class copyfile example
  /*
  copyfile copy1=new copyfile();
  boolean copy_ok=copy1.copy("c:/hello.jsp","c:/hello_backup.jsp");
  if(copy_ok)
  {
   out.println("拷贝成功!");
  }
  else
  {
   out.println("拷贝失败!");
  }
  */
  //-----------class copyfile end-----------

扫描关注微信公众号