import java.io.File;
import java.util.LinkedList;
public class DiguiDemo1 {
/**
*递归删除带内容的目录案例
*/
//删除lm目录下的所有文件
public static void main(String[] args) {
File file=new File("d:\\lm");
Track<File> track=new Track<File>();
track.add(file);
while (!track.isEmpty()) {
File f = track.get();
if (f.isDirectory()) {
track.add(f);
File[] fs = f.listFiles();
for (File ff : fs) {
track.add(ff);
}
} else {
System.out.println(f.getAbsolutePath());
f.delete();
}
}
file.delete();
}
}
class Track<T>
{
LinkedList<T> list=new LinkedList<T>();
public void add(T t)
{
list.addFirst(t);
}
public T get()
{
T t=list.removeLast();
return t;
}
public boolean isEmpty()
{
boolean flag=list.isEmpty();
return flag;
}
}
|
|