转载自http://edu.codepub.com/2010/0406/21654.php
递归删除目录就常见了,随便网上搜了一个过来,见下面
非递归的还真没写过。。随便写了一个。仅供参考
递归删除目录就常见了,随便网上搜了一个过来,见下面
import java.io.File;
class DeleteDir {
public static void main(String args[]) {
deleteDirectory(new File(args[0]));
}
static public boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
}
}
非递归的还真没写过。。随便写了一个。仅供参考
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Delete {
public static void main(String[] args)
{
File deletePath = new File("你要删除的目录位置");
List<String> deleteList = new ArrayList<String>();
deleteList.add(deletePath.getPath());
while(deleteList.size()>0)
{
int i = deleteList.size()-1;
String currentPath = deleteList.get(i);
File[] files = (new File(currentPath)).listFiles();
if (files.length == 0) {
(new File(currentPath)).delete();
deleteList.remove(i);
}else {
for (int k = 0; k < files.length; k++)
{
if(files[k].isDirectory())
{
System.out.println(files[k].getPath());
deleteList.add(files[k].getPath());
}
else
{
System.out.println(files[k].getName());
files[k].delete();
}
}
}
}
}
}