1. import java.io.BufferedInputStream;
    2. import java.io.BufferedOutputStream;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. public class FileCopyTools {
    6. public static void main(String[] args){
    7. copyFile("D:\\javaStudy\\javaworkspace/a.txt","d:/javaStudy/javaworkspace/b.txt");
    8. }
    9. /**
    10. * 文件拷贝方法,工具方法一般定义为静态方法
    11. * @param srcpath
    12. * @param despath
    13. */
    14. public static void copyFile(String srcpath,String despath){
    15. FileInputStream fis=null;
    16. BufferedInputStream bis=null;
    17. FileOutputStream fos=null;
    18. BufferedOutputStream bos=null;
    19. try{
    20. bis=new BufferedInputStream(new FileInputStream(srcpath));
    21. bos=new BufferedOutputStream(new FileOutputStream(despath));
    22. int temp=0;
    23. while((temp=bis.read())!=-1){
    24. bos.write(temp);
    25. }
    26. bos.flush();
    27. }catch (Exception e){
    28. e.printStackTrace();
    29. }finally {
    30. try{
    31. if(bis!=null){
    32. bis.close();
    33. }
    34. if(fis!=null){
    35. fis.close();
    36. }
    37. if(bos!=null){
    38. bos.close();
    39. }
    40. if(fos!=null){
    41. fos.close();
    42. }
    43. }catch (Exception e){
    44. e.printStackTrace();
    45. }
    46. }
    47. }
    48. }