博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中实现复制文件或文件夹——CopyUtil.java
阅读量:4676 次
发布时间:2019-06-09

本文共 2546 字,大约阅读时间需要 8 分钟。

参考自:

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

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

[java] 
  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. }  

转载于:https://www.cnblogs.com/yang3wei/archive/2012/03/21/2739827.html

你可能感兴趣的文章
一些简单有用的方法合集
查看>>
Neutron 架构 - 每天5分钟玩转 OpenStack(67)
查看>>
详解JS设计模式
查看>>
CPSR寄存器
查看>>
Java基础50题test7—处理字符串
查看>>
保险行业电话外呼型呼叫中心方案
查看>>
自建型呼叫中心
查看>>
input file 文件上传,js控制上传文件的大小和格式
查看>>
Day 6 函数与模块
查看>>
WebApi请求原理
查看>>
[Node.js] node-persist: localStorage on the server
查看>>
jquery.event 研究学习之bind篇
查看>>
LOJ #108. 多项式乘法
查看>>
libusb开发指南
查看>>
SAS基础 -- 逻辑库不存在问题解决
查看>>
Servlet监听器统计在线人数
查看>>
第2章 数字之魅——寻找发帖“水王”
查看>>
eclipse jsp html 格式化 format
查看>>
关于手机端IOS系统微信中虚拟键盘遮挡input输入框问题的解决方案 草稿
查看>>
css3背景、边框、和补丁相关属性 (二)
查看>>