java清理垃圾_自己写的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 HashMap map = new HashMap(); //删除结果分类

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+"秒");

}

}

1

1

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-01-09 20:08

浏览 655

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值