Java中实现复制文件或文件夹——CopyUtil.java

参考自:http://blog.csdn.net/Dream_JavaWorld/article/details/3682075

拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。

但是在复制文件夹时,则需要利用Flie类在目标文件夹中创建相应的目录,并且使用递归方法。

[java]  view plain copy
  1. package org.bruce.convert.util;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * 复制文件夹或文件夹 
  7.  */  
  8. public class CopyUtil {  
  9.     public static boolean _choice = true;  
  10.       
  11.     // 复制文件  
  12.     public static void CopyFile(File sourceFile, File targetFile)  
  13.             throws IOException {  
  14.         // 新建文件输入流并对它进行缓冲  
  15.         FileInputStream input = new FileInputStream(sourceFile);  
  16.         BufferedInputStream inBuff = new BufferedInputStream(input);  
  17.   
  18.         // 新建文件输出流并对它进行缓冲  
  19.         FileOutputStream output = new FileOutputStream(targetFile);  
  20.         BufferedOutputStream outBuff = new BufferedOutputStream(output);  
  21.   
  22.         // 缓冲数组  
  23.         byte[] b = new byte[1024 * 5];  
  24.         int len;  
  25.         while ((len = inBuff.read(b)) != -1) {  
  26.             outBuff.write(b, 0, len);  
  27.         }  
  28.         // 刷新此缓冲的输出流  
  29.         outBuff.flush();  
  30.   
  31.         // 关闭流  
  32.         inBuff.close();  
  33.         outBuff.close();  
  34.         output.close();  
  35.         input.close();  
  36.     }  
  37.   
  38.     // 复制文件夹  
  39.     public static void CopyDirectory(String sourceDir, String targetDir)  
  40.             throws IOException {  
  41.         // 新建目标目录  
  42.         (new File(targetDir)).mkdirs();  
  43.         // 获取源文件夹当前下的文件或目录  
  44.         File[] file = (new File(sourceDir)).listFiles();  
  45.         for (int i = 0; i < file.length; i++) {  
  46.             if (file[i].isFile()) {  
  47.                 // 源文件  
  48.                 File sourceFile = file[i];  
  49.                 // 目标文件  
  50.                 File targetFile = new File(  
  51.                         new File(targetDir).getAbsolutePath() + File.separator  
  52.                                 + file[i].getName());  
  53.                 CopyFile(sourceFile, targetFile);  
  54.             }  
  55.             if (file[i].isDirectory()) {  
  56.                 // 准备复制的源文件夹  
  57.                 String dir1 = sourceDir + File.separator + file[i].getName();  
  58.                 // 准备复制的目标文件夹  
  59.                 String dir2 = targetDir + File.separator + file[i].getName();  
  60.                 CopyDirectory(dir1, dir2);  
  61.             }  
  62.         }  
  63.     }  
  64.       
  65.     /** 
  66.      * 稍作包装, 
  67.      * 如果 url1 是文件的话,直接copyFile 
  68.      * 如果 url1 是文件夹的话,再 copyDirectory 
  69.      * @throws IOException  
  70.      */  
  71.      public static boolean PowerCopy(String source, String dest) {  
  72.          try {  
  73.             File input = new File(source);  
  74.              if(input.isFile() && !_choice) {  
  75.                  // 如果输入是一个文件~  
  76.                  (new File(dest)).mkdirs();  
  77.                  File output = new File(dest+File.separator+input.getName());  
  78.                  CopyFile(input, output);  
  79.              } else if(input.isFile() && _choice) {  
  80.                  CopyFile(new File(source), new File(dest));  
  81.              } else {  
  82.                  // 如果输入是一个文件夹~  
  83.                  CopyDirectory(source, dest);  
  84.              }  
  85.              return true;  
  86.         } catch (IOException e) {  
  87.             // TODO Auto-generated catch block  
  88.             e.printStackTrace();  
  89.             BY.Err("exception occured in CopyUtil.powerCopy!");  
  90.             return false;  
  91.         }  
  92.      }  
  93.       
  94.     /** 
  95.      * usage!! 
  96.      */  
  97.     public static void main(String args[]) throws IOException {  
  98.         // 单文件~  
  99.         String url1 = "/Users/user/Desktop/480*320.png";  
  100.         String url2 = "/Users/user/Desktop/481*320.png";  
  101.           
  102.         // 源文件夹 -> 目标文件夹  
  103. //      String url1 = "/Users/user/Desktop/Resources";  
  104. //      String url2 = "/Users/user/Desktop/Resources2";  
  105.         PowerCopy(url1, url2);  
  106.     }  
  107. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值