目录处理工具类 DealWithDir.java

  1. package com.util;  
  2.   
  3. import java.io.File;  
  4.   
  5. /** 
  6.  * 目录处理工具类 
  7.  * 
  8.  */  
  9. public class DealWithDir {  
  10.     /** 
  11.      * 新建目录 
  12.      */  
  13.     public static boolean newDir(String path) throws Exception {  
  14.         File file = new File(path);  
  15.         return file.mkdirs();//创建目录  
  16.     }  
  17.       
  18.     /** 
  19.      * 删除目录 
  20.      */  
  21.     public static boolean deleteDir(String path) throws Exception {  
  22.         File file = new File(path);  
  23.         if (!file.exists())  
  24.             return false;// 目录不存在退出  
  25.         if (file.isFile()) // 如果是文件删除  
  26.         {  
  27.             file.delete();  
  28.             return false;  
  29.         }  
  30.         File[] files = file.listFiles();// 如果目录中有文件递归删除文件  
  31.         for (int i = 0; i < files.length; i++) {  
  32.             deleteDir(files[i].getAbsolutePath());  
  33.         }  
  34.         file.delete();  
  35.           
  36.         return file.delete();//删除目录  
  37.     }  
  38.   
  39.     /** 
  40.      * 更新目录 
  41.      */  
  42.     public static boolean updateDir(String path, String newPath) throws Exception {  
  43.         File file = new File(path);  
  44.         File newFile = new File(newPath);  
  45.         return file.renameTo(newFile);  
  46.     }  
  47.       
  48.     public static void main(String d[]) throws Exception{  
  49.         //deleteDir("d:/ff/dddf");  
  50.         updateDir("D:\TOOLS\Tomcat 6.0\webapps\BCCCSM\nationalExperiment/22222", "D:\TOOLS\Tomcat 6.0\webapps\BCCCSM\nationalExperiment/224222");  
  51.     }  
  52.       
  53.       
  54. }  
原文地址:https://www.cnblogs.com/swite/p/5168706.html