爱编程的欧巴

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

文件/目录部分处理工具类 DealDir.java

Posted on 2016-01-29 13:18  爱编程的欧巴  阅读(247)  评论(0编辑  收藏  举报
  1. package com.util;  
  2.   
  3. import java.io.File;  
  4. import java.util.StringTokenizer;  
  5.   
  6. /** 
  7.  * 文件/目录 部分处理 
  8.  * @createTime Dec 25, 2010 7:06:58 AM 
  9.  * @version 1.0 
  10.  */  
  11. public class DealDir {  
  12.     /** 
  13.      * 获取文件的后缀名并转化成大写 
  14.      *  
  15.      * @param fileName 
  16.      *            文件名 
  17.      * @return 
  18.      */  
  19.     public String getFileSuffix(String fileName) throws Exception {  
  20.         return fileName.substring(fileName.lastIndexOf(".") + 1,  
  21.                 fileName.length()).toUpperCase();  
  22.     }  
  23.   
  24.     /** 
  25.      * 创建多级目录 
  26.      *  
  27.      * @param path 
  28.      *            目录的绝对路径 
  29.      */  
  30.     public void createMultilevelDir(String path) {  
  31.         try {  
  32.             StringTokenizer st = new StringTokenizer(path, "/");  
  33.             String path1 = st.nextToken() + "/";  
  34.             String path2 = path1;  
  35.             while (st.hasMoreTokens()) {  
  36.   
  37.                 path1 = st.nextToken() + "/";  
  38.                 path2 += path1;  
  39.                 File inbox = new File(path2);  
  40.                 if (!inbox.exists())  
  41.                     inbox.mkdir();  
  42.   
  43.             }  
  44.         } catch (Exception e) {  
  45.             System.out.println("目录创建失败" + e);  
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     /** 
  52.      * 删除文件/目录(递归删除文件/目录) 
  53.      *  
  54.      * @param path 
  55.      *            文件或文件夹的绝对路径 
  56.      */  
  57.     public void deleteAll(String dirpath) {  
  58.         if (dirpath == null) {  
  59.             System.out.println("目录为空");  
  60.         } else {  
  61.             File path = new File(dirpath);  
  62.             try {  
  63.                 if (!path.exists())  
  64.                     return;// 目录不存在退出  
  65.                 if (path.isFile()) // 如果是文件删除  
  66.                 {  
  67.                     path.delete();  
  68.                     return;  
  69.                 }  
  70.                 File[] files = path.listFiles();// 如果目录中有文件递归删除文件  
  71.                 for (int i = 0; i < files.length; i++) {  
  72.                     deleteAll(files[i].getAbsolutePath());  
  73.                 }  
  74.                 path.delete();  
  75.   
  76.             } catch (Exception e) {  
  77.                 System.out.println("文件/目录 删除失败" + e);  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     /** 
  84.      * 文件/目录 重命名 
  85.      *  
  86.      * @param oldPath 
  87.      *            原有路径(绝对路径) 
  88.      * @param newPath 
  89.      *            更新路径 
  90.      * @author lyf 注:不能修改上层次的目录 
  91.      */  
  92.     public void renameDir(String oldPath, String newPath) {  
  93.         File oldFile = new File(oldPath);// 文件或目录  
  94.         File newFile = new File(newPath);// 文件或目录  
  95.         try {  
  96.             boolean success = oldFile.renameTo(newFile);// 重命名  
  97.             if (!success) {  
  98.                 System.out.println("重命名失败");  
  99.             } else {  
  100.                 System.out.println("重命名成功");  
  101.             }  
  102.         } catch (RuntimeException e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.   
  106.     }  
  107.   
  108. }