Board logo

标题: 自己写的Java垃圾文件清理 [打印本页]

作者: qingqing3721    时间: 2011-6-6 23:57     标题: 自己写的Java垃圾文件清理

自己没事儿写的垃圾文件道理, 算法很垃圾。 对付着能用。
  =====================================================================
  package com. datatransfer;
  import java. io. File;
  import java. io. IOException;
  import java. text. SimpleDateFormat;
  import java. util. Date;
  import java. util. HashMap;
  import javax. swing. JFileChooser;
  public class test {
  // tmp *. _mp *. log *. gid *. chk *. old *. dmp *. bak *. hlp temp internet
  static String[] SYSARRAY = { "tmp",  "_mp",  "gid",  "chk",  "old",
  "dmp",  "bak",  "hlp",  "temp",  "internet" };
  static HashMapString, Integer map = new HashMapString, Integer(); //删除结果分类
  static StringBuffer sb = new StringBuffer(); //删除结果详细
  static long count = 0; //删除个数
  static long scanCount = 0; //扫描个数
  static long node = 0; //子文件夹
  /**
  * @param args
  * @author 2010-3-30上午10:06:06By Simon (赵振明).
  * @throws IOException
  */
  public static void main(String[] args) throws IOException {
  test t = new test();
  java. util. Date now = new Date();
  t. listFile("D:\\");
  java. util. Date date=new Date();
  TDoA(date, now);
  System. err. println("共扫描 :" + scanCount + "个文件");
  System. err. println("共删除了 :" + count + "个文件");
  for (String str : SYSARRAY) {
  System. err. println("共删除【"+str+"】类型垃圾文件 :"+map. get(str)+"个");
  }
  System. err. println("=====================详细信息=====================");
  System. err. println(sb. toString());
  }
  /**
  * 获取文件夹下的所有文件
  *
  * @author 2010-3-30上午10:56:51By Simon (赵振明).
  */
  public void listFile(String pash) {
  File f = new File(pash);
  File[] array = f. listFiles();
  //JFileChooser j = new JFileChooser();
  for (File file : array) {
  if (file. isDirectory()) {
  this. File(file);
  } else {
  System. out. println("扫描文件:" + file. getPath());
  if (trimExtension(file. getName())) {
  // System. err. println("删除 :"+file. getPath());
  file. delete();
  count++;
  sb. append("删除了:" + file. getPath());
  sb. append("/n");
  }
  // System. err. println(j. getTypeDescription(file));
  }
  }
  } /***
  * 扫描子目录 文件
  * @param f
  * @author 2010-3-30下午05:44:03By Simon (赵振明).
  */
  public void File(File f) {
  File[] array = f. listFiles();
  //JFileChooser j = new JFileChooser();
  if (null != array) {
  for (File file : array) {
  if (file. isDirectory()) {
  this. File(file);
  } else {
  // System. err. println(file. getName());
  // System. err. println(j. getTypeDescription(file));
  System. out. println("扫描文件:" + file. getPath());
  if (trimExtension(file. getName())) {
  // System. err. println("删除 :"+file. getPath());
  file. delete();
  count++;
  sb. append("成功删除了:" + file. getPath());
  sb. append("\n");
  }
  }
  }
  }
  }
  /***
  * 截取文件名的后缀命
  *
  * @param filename
  * @return
  * @author 2010-3-30下午02:32:41By Simon (赵振明).
  */
  public static boolean trimExtension(String filename) {
  scanCount++;
  boolean fig = false;
  if ((filename != null)  (filename. length()  0)) {
  int i = filename. lastIndexOf('. ');
  if ((i  -1)  (i  (filename. length()))) {
  fig = isDelFile(filename. substring(i + 1,  filename. length()));
  } else {
  return fig;
  }
  }
  return fig;
  }
  /***
  * 判断文件是否属于黑名单范围
  *
  * @param name
  * @return
  * @author 2010-3-30下午02:34:34By Simon (赵振明).
  */
  public static boolean isDelFile(String name) {
  for (String nodename : SYSARRAY) {
  if (nodename. equals(name)) {
  if(null!=map. get(nodename)){
  map. put(nodename, map. get(nodename)+1);
  }else{
  map. put(nodename, 1);
  }
  return true;
  }
  }
  return false;
  }
  /***
  * 时间差计算
  * @param now
  * @param date
  * @author 2010-3-30下午05:43:11By Simon (赵振明).
  */
  public static void TDoA(Date now , Date date){
  long l=now. getTime()-date. getTime();
  long day=l/(24*60*60*1000);
  long hour=(l/(60*60*1000)-day*24);
  long min=((l/(60*1000))-day*24*60-hour*60);
  long s=(l/1000-day*24*60*60-hour*60*60-min*60);
  System. out.println("历时:"+day+"天"+hour+"小时"+min+"分"+s+"秒");
  }
  }文章由御淑堂丰胸产品整理,收集辛苦,希望能保留出处。




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0