爱编程的欧巴

让我们成长吧~
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

目录操作工具类 CopyDir.java

Posted on 2016-01-29 13:15  爱编程的欧巴  阅读(511)  评论(0编辑  收藏  举报
  1. package com.util;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * 1,建立目的目录。 2,遍历源目录。 3,遍历过程中,创建文件或者文件夹。 原理:其实就是改变了源文件或者目录的目录头。 
  7.  * @datetime  Dsc  24 
  8.  */  
  9. public class CopyDir {  
  10.     private File sDir, dDir, newDir;  
  11.   
  12.     public CopyDir(String s, String d) {  
  13.         this(new File(s), new File(d));  
  14.     }  
  15.   
  16.     CopyDir(File sDir, File dDir)// c:\\Test d:\\abc  
  17.     {  
  18.         this.sDir = sDir;  
  19.         this.dDir = dDir;  
  20.     }  
  21.   
  22.     public void copyDir() throws IOException {  
  23.         // 是创建目的目录。也就是创建要拷贝的源文件夹。Test  
  24.         // 获取源文件夹名称。  
  25.         String name = sDir.getName();  
  26.         // 通过该名称在目的目录创建该文件夹,为了存放源文件夹中的文件或者文件夹。  
  27.         // 将目的目录和源文件夹名称,封装成File对象。  
  28.         newDir = dDir;  
  29.         // new File(dDir,name);  
  30.         // 调用该对象的mkdir方法。在目的目录创建该文件夹。d:\\abc\\Test  
  31.         newDir.mkdir();//  
  32.   
  33.         // 遍历源文件夹。  
  34.         listAll(sDir);  
  35.     }  
  36.   
  37.     /* 
  38.      * 将遍历目录封装成方法。 在遍历过程中,遇到文件创建文件。 遇到目录创建目录。 
  39.      */  
  40.     private void listAll(File dir) throws IOException {  
  41.         File[] files = dir.listFiles();  
  42.         for (int x = 0; x < files.length; x++) {  
  43.             if (files[x].isDirectory()) {  
  44.                 createDir(files[x]);// 调用创建目录的方法。  
  45.                 listAll(files[x]);// 在继续进行递归。进入子级目录。  
  46.             } else {  
  47.                 createFile(files[x]);// 调用创建文件的方法。  
  48.             }  
  49.         }  
  50.     }  
  51.   
  52.     /* 
  53.      * copy目录。通过源目录在目的目录创建新目录。 
  54.      */  
  55.     private void createDir(File dir) {  
  56.         File d = replaceFile(dir);  
  57.         d.mkdir();  
  58.     }  
  59.   
  60.     /* 
  61.      * copy文件。 
  62.      */  
  63.     private void createFile(File file) throws IOException {  
  64.         File newFile = replaceFile(file);  
  65.         // copy文件是一个数据数据传输的过程。需要通过流来完成。  
  66.         FileInputStream fis = new FileInputStream(file);  
  67.         FileOutputStream fos = new FileOutputStream(newFile);  
  68.         byte[] buf = new byte[1024 * 2];  
  69.         int num = 0;  
  70.         while ((num = fis.read(buf)) != -1) {  
  71.             fos.write(buf, 0, num);  
  72.         }  
  73.         fos.close();  
  74.         fis.close();  
  75.     }  
  76.   
  77.     /* 
  78.      * 替换路径。 
  79.      */  
  80.     private File replaceFile(File f) {  
  81.         // 原理是:将源目录的父目录(C:\\Tset),替换成目的父目录。(d:\\abc\\Test)  
  82.         String path = f.getAbsolutePath();// 获取源文件或者文件夹的决定路径。  
  83.         // 将源文件或者文件夹的绝对路径替换成目的路径。  
  84.         String newPath = path.replace(sDir.getAbsolutePath(), newDir  
  85.                 .getAbsolutePath());  
  86.         // 将新的目的路径封装成File对象  
  87.         File newFile = new File(newPath);  
  88.         return newFile;  
  89.     }