自己沒事兒寫的垃圾文件情理,演算法很垃圾.湊合著能用.
=====================================================================
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 HashMap<String,Integer> map = new HashMap<String,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 "秒");
}
}
[火星人 ] 自己寫的Java垃圾文件清理已經有421次圍觀