JAVA拷贝

Java代码 复制代码 收藏代码
  1. /*
  2. *Tochangethistemplate,chooseTools|Templates
  3. *andopenthetemplateintheeditor.
  4. */
  5. packagecn.lsd;
  6. importjava.io.File;
  7. importjava.io.FileInputStream;
  8. importjava.io.FileNotFoundException;
  9. importjava.io.FileOutputStream;
  10. importjava.io.IOException;
  11. importjava.util.logging.Level;
  12. importjava.util.logging.Logger;
  13. /**
  14. *
  15. *@date2008-11-26
  16. *@authorlsd
  17. */
  18. publicclassCpp{
  19. privateStringsourceFile;
  20. privateStringdestFile;
  21. publicCpp(StringsourceFile,StringdestFile){
  22. this.sourceFile=sourceFile;
  23. this.destFile=destFile;
  24. doCopy();
  25. }
  26. privatevoiddoCopy(){
  27. FileInputStreamin=null;
  28. FileOutputStreamout=null;
  29. byte[]buffer=newbyte[102400];
  30. try{
  31. in=newFileInputStream(this.sourceFile);
  32. Filedest=newFile(this.destFile);
  33. if(!dest.exists()){//目标文件对应的目录不存在,创建新的目录
  34. intindex=newString(this.destFile).lastIndexOf("/");
  35. Stringpath=this.destFile.substring(0,index);
  36. newFile(path).mkdirs();
  37. }
  38. out=newFileOutputStream(this.destFile);
  39. intnum=0;
  40. while((num=in.read(buffer))!=-1){
  41. out.write(buffer,0,num);
  42. }
  43. }catch(FileNotFoundExceptionex){
  44. Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE,null,ex);
  45. }catch(IOExceptione){
  46. Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE,null,e);
  47. }finally{
  48. try{
  49. if(in!=null)
  50. in.close();
  51. if(out!=null)
  52. out.close();
  53. }catch(IOExceptionex){
  54. Logger.getLogger(Cpp.class.getName()).log(Level.SEVERE,null,ex);
  55. }
  56. }
  57. }
  58. publicstaticvoidmain(String[]args){
  59. StringsourceFile="d:/颐和园yhy[小新奉献].rmvb";
  60. StringdestFile="D:/s/sd/1.rmvb";
  61. longstartTime=System.currentTimeMillis();
  62. System.out.println("starttocopy");
  63. Cppc=newCpp(sourceFile,destFile);
  64. longendTime=System.currentTimeMillis();
  65. longtime=(endTime-startTime)/1000;
  66. System.out.println("copyend;cost:"+time);
  67. }
  68. }

你可能感兴趣的:(java)