java删除thread_Java Thread 删除目录文件

Java Thread

中间几个函数是从网上copy的,改了改。

1.[代码][Java]代码

import java.io.*;

class TestThread extends Thread {

private String[] args;

public boolean deleteFile(String sPath) {

boolean flag = false;

File file = new File(sPath);

// 路径为文件且不为空则进行删除

if (file.isFile() && file.exists()) {

file.delete();

flag = true;

}

return flag;

}

public boolean deleteDirectory(String sPath) {

//如果sPath不以文件分隔符结尾,自动添加文件分隔符

if (!sPath.endsWith(File.separator)) {

sPath = sPath + File.separator;

}

File dirFile = new File(sPath);

//如果dir对应的文件不存在,或者不是一个目录,则退出

if (!dirFile.exists() || !dirFile.isDirectory()) {

return false;

}

boolean flag = true;

//删除文件夹下的所有文件(包括子目录)

File[] files = dirFile.listFiles();

for (int i = 0; i < files.length; i++) {

if (files[i].isFile()) {

flag = deleteFile(files[i].getAbsolutePath());

if (!flag) break;

} else {

flag = deleteDirectory(files[i].getAbsolutePath());

if (!flag) break;

}

}

if (!flag) return false;

//删除当前目录

if (dirFile.delete()) {

return true;

} else {

return false;

}

}

public boolean DeleteFolder(String sPath) {

boolean flag = false;

File file = new File(sPath);

// 判断目录或文件是否存在

if (!file.exists()) { // 不存在返回 false

System.out.println(sPath + " not exist");

return flag;

} else {

// 判断是否为文件

if (file.isFile()) { // 为文件时调用删除文件方法

return deleteFile(sPath);

} else { // 为目录时调用删除目录方法

return deleteDirectory(sPath);

}

}

}

public TestThread(String[] args) {

this.args = args;

}

public void run() {

int i;

for (i = 0; i < args.length; i++) {

System.out.println("Delete " + args[i]);

DeleteFolder(args[i]);

}

}

}

public class Del {

public static void main(String[] args) {

new TestThread(args).start();

System.out.println(Runtime.getRuntime().availableProcessors() + " cpus");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值