您的位置:首页 > 其它

删除指定时间前目录下生成的所有文件、文件夹

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐