JAVA实现复制文件夹

  1 package com.filetest;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 import java.util.Scanner;
 10 
 11 public class copyfile {
 12 
 13     public static void main(String args[]) throws IOException{
 14         
 15         copy();            //调用复制函数
 16         System.out.println("复制完成!");    //提示复制完成
 17     }
 18 
 19     private static void copy() throws IOException {
 20         
 21         System.out.println("输入你要复制的文件路径及名称:");
 22         Scanner scanner=new Scanner(System.in);
 23         String oldpath=scanner.next();        //接收原文件夹路径及名称
 24         
 25         File ofile=new File(oldpath);    
 26         if((!ofile.exists()||!ofile.isDirectory())){    //判断源文件夹路径是否存在
 27             
 28             if(oldpath.equals("end"))                    //路径不存在则进入判断内,如果输入的不是end则递归调用重新输入
 29             {
 30                 System.out.println("程序结束,感谢使用!");
 31                 System.exit(-1);
 32             }
 33             else
 34             {
 35                 System.out.println("输入的源文件夹路径不存在,请重新输入!(输入end退出程序)");
 36                 copy();
 37             }
 38                         
 39         }
 40         
 41         System.out.println("输入你要复制该文件到哪个路径:");
 42         String newpath=scanner.next();        //接收目标文件夹路径及名称
 43         
 44         File nfile=new File(newpath);
 45         if(!nfile.isAbsolute()){    //判断目标文件夹路径是否为目录
 46             if(newpath.equals("end"))                    //路径不存在则进入判断内,如果输入的不是end则递归调用重新输入
 47             {
 48                 System.out.println("程序结束,感谢使用!");
 49                 System.exit(-1);
 50             }
 51             else
 52             {
 53                 System.out.println("输入的目标文件夹目录格式不正确,请重新输入!(输入end退出程序)");
 54                 copy();
 55             }
 56                         
 57         }
 58         
 59         //截取源文件夹路径最后的名字
 60         String laststr  = oldpath.substring(oldpath.lastIndexOf("/"), oldpath.length());
 61         copyDirectiory(oldpath,newpath+"/"+laststr);  //将原路径文件夹名称和目标路径文件夹名称传递给复制文件夹函数
 62         
 63         
 64     }
 65     
 66     
 67     //用缓冲流复制文件函数
 68     public static void copyFile(File sourceFile,File targetFile)   
 69             throws IOException{  
 70                     // 新建文件输入流并对它进行缓冲   
 71                     FileInputStream input = new FileInputStream(sourceFile);  
 72                     BufferedInputStream inBuff=new BufferedInputStream(input);  
 73               
 74                     // 新建文件输出流并对它进行缓冲   
 75                     FileOutputStream output = new FileOutputStream(targetFile);  
 76                     BufferedOutputStream outBuff=new BufferedOutputStream(output);  
 77                       
 78                     int len;  
 79                     while ((len =inBuff.read()) != -1) 
 80                     {  
 81                         outBuff.write(len);  
 82                     }  
 83                     // 刷新此缓冲的输出流   
 84                     outBuff.flush();  
 85                       
 86                     //关闭流   
 87                     inBuff.close();  
 88                     outBuff.close();  
 89                     output.close();  
 90                     input.close();  
 91                 }  
 92     
 93     
 94     // 复制文件夹函数
 95     public static void copyDirectiory(String sourceDir, String targetDir)  
 96                         throws IOException {                             
 97                          
 98                         File aimfile=new File(targetDir);
 99                         if(!(aimfile).exists()){    //查看目录是否存在,不存在则新建
100                              aimfile.mkdirs();   
101                         }
102                         
103                         if(sourceDir.equals(targetDir)){    //如果文件路径及文件名相同则覆盖
104                             System.out .println("文件已存在,是否覆盖(N退出/任意键继续)?");
105                             Scanner scanner=new Scanner(System.in);
106                             String NY=scanner.next();
107                             if(NY.equalsIgnoreCase("n")){    //如果不想覆盖 可退出程序
108                                 System.out.println("程序结束,感谢使用!");
109                                 System.exit(-1);
110                             }
111 
112                         }
113                     
114                         // 获取源文件夹下的文件或目录   
115                         File oldfile=new File(sourceDir);
116                         File[] file=oldfile.listFiles();  
117         
118                         for(int i=0;i<file.length;i++)
119                         {  
120                             
121                             if (file[i].isFile()) //如果是文件,传递给copyFile()函数进行复制
122                                 {   
123                                     //目标文件  
124                                     File aim=new File(targetDir);  
125                                     File targetFile=new File(aim.getAbsolutePath()+"/"+file[i].getName());  
126                                     copyFile(file[i],targetFile);  
127                                 }  
128                             if (file[i].isDirectory()) //如果是文件夹,则递归调用
129                                 {  
130                                     // 要递归复制的源文件夹   
131                                     String soursefiles=sourceDir + "/" + file[i].getName(); 
132                                     
133                                     // 要递归复制的目标文件夹   
134                                     String aimfiles=targetDir + "/"+ file[i].getName();
135                                     
136                                     copyDirectiory(soursefiles, aimfiles);  
137                                 }  
138                         }  
139                 }  
140         }


其实在复制单个文件的时候可以优化一下  用FileChannel比缓冲复制效率高三分之一。仅供大家参考。

posted @ 2014-07-20 22:51  云谷子  阅读(6498)  评论(0编辑  收藏  举报