zoukankan      html  css  js  c++  java
  • 目录操作工具类 CopyDir.java

    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.     }  
  • 相关阅读:
    Radmin View3.5
    delphi安装fastreport6
    大华监控设备配置文件导入生成工具
    大华监控和天地伟业监控免输密码登录助手
    锐捷交换机常用配置命令【加精】
    锐捷交换机的配置命令大全
    H3C常用配置命令
    锐捷交换机配置命令
    Delphi 随手笔记,使用了DEV控件组件
    读取软件的版本信息 GetFileVersionInfo
  • 原文地址:https://www.cnblogs.com/swite/p/5168694.html
Copyright © 2011-2022 走看看