删除指定时间前目录下生成的所有文件、文件夹
2010-06-19 11:14
801 查看
/* * 删除指定时间前目录下生成的所有文件、文件夹。 * 如果我有一个这样的目录这个目录是按照年月日时分秒生成C:/saveFile/2010y/06m/01d/15h/47mi这是最新的一个目录, * 然后我设定一个分钟10分钟,指定删除十分钟以前的所有的文件、文件夹 */ public class DeleteFilesJob implements Job { public void deleteFiles(){ Calendar ca = null; String str = null; ca = Calendar.getInstance(); File fileD = new File("C://saveFile"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/hh/mm"); String tmp = this.getCurrentCalendar(fileD, new StringBuffer()); if (tmp == null || tmp.equals("")) { log.info("没有目录"); return; } Date d = null; String ss = tmp.substring(0, tmp.length() - 1); try { d = sdf.parse(ss); } catch (ParseException e1) { log.error(e1.getMessage(),e1); d=new Date(); } log.info(sdf.format(d)); ca.setTime(d); ca.add(Calendar.MINUTE, -10); str = "mi"; try { this.getDeleteFile(fileD, 0, ca, str); } catch (IOException e) { // TODO Auto-generated catch block log.error(e.getMessage(), e); } } /* * 获取最新的的时间,然后组成路径 */ public String getCurrentCalendar(File file, StringBuffer sb) { File[] files = null; if (file.isDirectory()) { files = file.listFiles(); } int max = 0; File tmp = null; String maxString = null; if (files == null || files.length == 0) { return ""; } for (int i = 0; i < files.length; i++) { String fileName = files[i].getName(); if (fileName.length() == 32) { return ""; } String t = this.replaceAll(fileName); if (Integer.valueOf(t) > max) { max = Integer.valueOf(t); tmp = files[i]; } } maxString = this.replaceAll(tmp.getName()); sb.append(maxString).append("/"); this.getCurrentCalendar(tmp, sb); return sb.toString(); } /* * 获取需要删除的文件路径 */ private long getDeleteFile(File fileD, long size, Calendar ca, String str) throws IOException { long pSize = 0; File maxFile = null; File[] files = fileD.listFiles(); boolean flag = true; for (int i = 0; i < files.length; i++) { String fileName = files[i].getName(); if ((i == (files.length - 1)) && fileName.endsWith(str)) { flag = false; } if (fileName.length() == 32) { continue; } String maxName = this.getMax(fileName, ca); String replaceName = this.replaceAll(fileName); if (Integer.valueOf(replaceName) < Integer.valueOf(maxName) && flag) { pSize += FileUtils.sizeOfDirectory(files[i]); FileUtils.deleteDirectory(files[i]); log.info("is delete " + files[i].getPath()); } if (replaceName.equals(maxName)) { maxFile = files[i]; } } if (maxFile == null) { return size; } size += this.getDeleteFile(maxFile, pSize, ca, str); return size; } private String replaceAll(String fileName) { return fileName.replaceAll("//D", ""); } private String getMax(String fileName, Calendar ca) { String result = ""; if (fileName.endsWith("y")) { result = ca.get(Calendar.YEAR) + ""; } if (fileName.endsWith("m")) { result = this.getString((ca.get(Calendar.MONTH) + 1) + ""); } if (fileName.endsWith("d")) { result = this.getString(ca.get(Calendar.DAY_OF_MONTH) + ""); } if (fileName.endsWith("h")) { result = this.getString(ca.get(Calendar.HOUR_OF_DAY) + ""); } if (fileName.endsWith("mi")) { result = this.getString(ca.get(Calendar.MINUTE) + ""); } return result; } private String getString(String str) { if (str.length() == 1) { return "0" + str; } return str; } }
原文链接:http://hudong.javaeye.com/blog/694100
相关文章推荐
- 递归删除指定目录下的指定文件夹及其所有文件
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- File,把指定的目录下的所有文件和文件夹删除的方法
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- C#IO操作,删除指定目录下的所有文件夹和文件
- C# 删除指定目录下的 所有指定文件、文件夹
- 这是一个定时脚本,主要功能是遍历该文件夹下的所有文件并存储到数组,对数据中的文件进行操作,一个一个移动到指定的目录下,并删除原有文件
- ASP.NET 删除指定目录下的所有文件和文件夹
- php将某个指定目录下的所有文件及文件夹删除
- 批量删除当前目录下所有文件夹中的指定文件
- 删除指定目录下的所有文件和文件夹
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- asp.net 删除文件夹,指定文件夹,删除文件夹和所有文件,删除权限设置,递归删除文件夹目录及文件
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- C# 删除、遍历指定目录下的所有指定文件、文件夹
- 删除指定文件夹内创建时间24小时之外的所有文件
- C#直接删除指定目录下的所有文件及文件夹(保留目录)
- HDFS练习5-----删除指定目录下的所有的空文件和空文件夹(包含子文件夹下的内容)
- [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)
- PHP删除指定目录下所有文件夹以及文件