指定某目录某文件夹备份

这个方法是从CSDN看到的,后来我改了下指定某个目录复制呵呵。

package com.dengzy.beifen;

import java.io.*;

/**
 * 复制文件夹或文件夹
 * @author Dengzy
 * @version final max1.xx
 */
public class CopyDirectory {
 
 /**
  * 用来才是的main方法
  * @param args
  * @throws IOException
  */
 public static void main(String args[]) throws IOException {
  String oldPath = "D://kankan//One";
  String newPath = "D://TDDOWNLOAD";
  moveProjectjar(oldPath, newPath);
 }

 /**
  * 任务调度时,任务调度的接口方法
  * @param oldPath 这个路径是指要备份的kankan目录下的One文件夹,而不备份其他的文件夹
  * @param newPath 这个路径是指要把One的文件夹放到哪个文件下保存
  * @throws IOException
  */
 public static void moveProjectjar(String oldPath,String newPath) throws IOException{
  File oldFile = new File(oldPath);
  String oldFileName = oldFile.getName();
  
  //创建目标文件夹
  newPath = newPath+File.separator+oldFileName;
  new File(newPath).mkdirs();
  
  //此处要注意,newPath+File.separator
  bakProject(oldPath,newPath+File.separator);
 }
 
 /**
  * 任务调度中接口中调用的方法
  * @param oldPath 这个路径是指要备份的kankan目录下的One文件夹,而不备份其他的文件夹
  * @param newPath 这个路径是指要把在目标目录成功创建One文件夹后的路径
  * @throws IOException
  */
 private static void bakProject(String oldPath, String newPath) throws IOException {
  // 获取源文件夹当前下的文件或目录
  System.out.println("OldPath=="+oldPath);
  File[] file = (new File(oldPath)).listFiles();
  for (int i = 0; i < file.length; i++) {
   if (file[i].isFile()) {
    // 复制文件
    copyFile(file[i], new File(newPath + file[i].getName()));
   }
   if (file[i].isDirectory()) {
    // 复制目录
    String sourceDir = oldPath + File.separator + file[i].getName();
    String targetDir = newPath + file[i].getName();
    copyDirectiory(sourceDir, targetDir);
   }
  }
 }

 // 复制文件
 public static void copyFile(File sourceFile, File targetFile)
   throws IOException {
  // 新建文件输入流并对它进行缓冲
  FileInputStream input = new FileInputStream(sourceFile);
  BufferedInputStream inBuff = new BufferedInputStream(input);

  // 新建文件输出流并对它进行缓冲
  FileOutputStream output = new FileOutputStream(targetFile);
  BufferedOutputStream outBuff = new BufferedOutputStream(output);

  // 缓冲数组
  byte[] b = new byte[1024 * 5];
  int len;
  while ((len = inBuff.read(b)) != -1) {
   outBuff.write(b, 0, len);
  }
  // 刷新此缓冲的输出流
  outBuff.flush();

  // 关闭流
  inBuff.close();
  outBuff.close();
  output.close();
  input.close();
 }

 // 复制文件夹
 public static void copyDirectiory(String sourceDir, String targetDir)
   throws IOException {
  // 新建目标目录
  (new File(targetDir)).mkdirs();
  // 获取源文件夹当前下的文件或目录
  File[] file = (new File(sourceDir)).listFiles();
  for (int i = 0; i < file.length; i++) {
   if (file[i].isFile()) {
    // 源文件
    File sourceFile = file[i];
    // 目标文件
    System.out.println(new File(targetDir).getAbsolutePath()+ File.separator + file[i].getName());
    File targetFile = new File(new File(targetDir)
      .getAbsolutePath()
      + File.separator + file[i].getName());
    copyFile(sourceFile, targetFile);
   }
   if (file[i].isDirectory()) {
    // 准备复制的源文件夹
    String dir1 = sourceDir + File.separator + file[i].getName();
    // 准备复制的目标文件夹
    String dir2 = targetDir + File.separator + file[i].getName();
    copyDirectiory(dir1, dir2);
   }
  }
 }
}

 

 

http://aviva.javaeye.com/blog/360755

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值