java中文件的操作

来源:互联网 发布:access数据库编程 编辑:程序博客网 时间:2024/05/11 16:04

对文件的操作,大家都可能会写一些操作,其实Apache中FileUtils已经做了很好的封装,今天将常用的操作记录如下:

(1)保留规定的前几个月的记录如下:

 

Java代码
  1. /**  
  2.      * 清理目录中过期的文件  
  3.      *   
  4.      * @param dayCount  
  5.      *            :保存时间天数  
  6.      * @param dirPath  
  7.      *            :目录路径  
  8.      * @return  
  9.      */  
  10.     @SuppressWarnings("unchecked")   
  11.     private static boolean deleteFiles(String dirPath, String backupPath, int days, boolean isBackup) {   
  12.   
  13.         // 计算备份日期,备份该日期之前的文件   
  14.         Date pointDate = new Date();   
  15.         long timeInterval = pointDate.getTime() - convertDaysToMilliseconds(days);   
  16.         pointDate.setTime(timeInterval);   
  17.   
  18.         // 是否进行备份   
  19.         if (isBackup) {   
  20.             if (!backUpFiles(dirPath, backupPath, pointDate)) {   
  21.                 System.out.println(" Backup failed: " + dirPath);   
  22.                 return false;   
  23.             }   
  24.         }   
  25.   
  26.         // 设置文件过滤条件   
  27.         IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);   
  28.         IOFileFilter fileFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, timeFileFilter);   
  29.   
  30.         // 删除符合条件的文件   
  31.         File deleteRootFolder = new File(dirPath);   
  32.         Iterator itFile = FileUtils.iterateFiles(deleteRootFolder, fileFiles, TrueFileFilter.INSTANCE);   
  33.         while (itFile.hasNext()) {   
  34.             File file = (File) itFile.next();   
  35.             boolean result = file.delete();   
  36.             if (!result) {   
  37.                 LogEx.error("Failed to delete file of :" + file);   
  38.                 return false;   
  39.             }   
  40.         }   
  41.   
  42.         // 清理空的文件夹   
  43.         File[] forderList = deleteRootFolder.listFiles();   
  44.         if (forderList != null && forderList.length > 0) {   
  45.             for (int i = 0; i < forderList.length; i++) {   
  46.                 deleteEmptyDir(forderList[i]);   
  47.             }   
  48.         }   
  49.   
  50.         return true;   
  51.     }   
  52.   
  53.     /**  
  54.      * 备份删除文件到指定的目录 ,目录格式:yyyy_MM_dd_bak  
  55.      *   
  56.      * @param srcDir  
  57.      *            :源文件路径  
  58.      * @param destDir  
  59.      *            :目标文件路径  
  60.      * @param dayCount  
  61.      *            :时间间隔,备份该时间之前的数据  
  62.      * @return  
  63.      */  
  64.     private static boolean backUpFiles(String srcDir, String destDir, Date pointDate) {   
  65.         try {   
  66.             // 设置备份文件夹格式YYYY_MM_dd_bak   
  67.             SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");   
  68.             String folderName = format.format(new Date()) + "_bak";   
  69.   
  70.             File resFile = new File(srcDir);   
  71.             File distFile = new File(destDir + File.separator + folderName);   
  72.   
  73.             // 文件过滤条件   
  74.             IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);   
  75.             IOFileFilter fileFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, timeFileFilter);   
  76.   
  77.             // 复制文件目录   
  78.             FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, fileFiles);   
  79.             FileUtils.copyDirectory(resFile, distFile, filter, true);   
  80.   
  81.         } catch (IOException e) {   
  82.             e.printStackTrace();   
  83.             LogEx.error("Failed to backupFile:" + e.getMessage());   
  84.             return false;   
  85.         }   
  86.         return true;   
  87.     }   
  88.   
  89.     /**  
  90.      * 天与毫秒的转换  
  91.      *   
  92.      * @param days  
  93.      * @return  
  94.      */  
  95.     private static long convertDaysToMilliseconds(int days) {   
  96.         return days * 24L * 3600 * 1000;   
  97.     }  

 2、清理所有的文件夹

Java代码
  1.     private static void deleteEmptyDir(File dir) {   
  2.         if (dir.isDirectory()) {   
  3.             File[] fs = dir.listFiles();   
  4.             if (fs != null && fs.length > 0) {   
  5.                 for (int i = 0; i < fs.length; i++) {   
  6.                     File tmpFile = fs[i];   
  7.                     if (tmpFile.isDirectory()) {   
  8.                         deleteEmptyDir(tmpFile);   
  9.                     }   
  10.                     if (tmpFile.isDirectory() && tmpFile.listFiles().length <= 0) {   
  11.                         tmpFile.delete();   
  12.                     }   
  13.                 }   
  14.             }   
  15.             if (dir.isDirectory() && dir.listFiles().length == 0) {   
  16.                 dir.delete();   
  17.             }   
  18.         }   
  19.     }  
原创粉丝点击