递归遍历,或者利用swing里的treenode也许行
access(MyFile file){
if(file.isLastFile())
return;
if(file.isFolder()){
access(file.getFirstChildFile());
access(file.getNextSiblingFolder());
}
else{
//访问文件的代码
access(file.getNextSiblingFile());
}
}
大概类似这样一个代码吧,实现深度优先遍历。若要广度优先就是建一个队列,把访问到的文件/文件夹都放在队尾,从队头取,队列变空的时候就遍历完了,呵呵。
access(MyFile file){
if(file.isLastFile())
return;
if(file.isFolder()){
access(file.getFirstChildFile());
access(file.getNextSiblingFolder());
}
else{
//访问文件的代码
access(file.getNextSiblingFile());
}
}
大概类似这样一个代码吧,实现深度优先遍历。若要广度优先就是建一个队列,把访问到的文件/文件夹都放在队尾,从队头取,队列变空的时候就遍历完了,呵呵。
private boolean Deldir(String vpath) {
String pathName = "";
try {
pathName = vpath;
File delPath = new File(pathName);
if (delPath.exists()) {
String[] strList;
strList = delPath.list();
for (int i = 0; i < strList.length; i++) {
String fileTree = pathName + "/" + strList[i];
File turePath = new File(fileTree);
boolean isdir = turePath.isDirectory();
if (isdir)
Deldir(fileTree);
else
turePath.delete();
}
if (!this.path.equals(vpath)) //this.path为输入的路径,但因为本身是递归,所以不能存放在这个方法中,我是存放在类中的
delPath.delete();
return true;
}
} catch (Exception e) {
return false;
}
return false;
}