用jsp如何实现删除整个文件夹,及文件夹下所有的文件?
用jsp如何实现删除整个文件夹,及文件夹下所有的文件?
小弟初学jsp,在线急等。谢谢
问题点数:20、回复次数:12Top
1 楼wu_lin_326()回复于 2005-04-12 08:36:39 得分 0
?????? 呜呜Top
2 楼andy126wb(陪你看海)回复于 2005-04-12 08:48:22 得分 20
用一下递归方法:
private void deleteFolder(File folder) {
String childs[] = folder.list();
if (childs == null || childs.length <= 0) {
folder.delete();
}
for (int i = 0; i < childs.length; i++) {
String childName = childs[i];
String childPath = folder.getPath() + File.separator + childName;
File filePath = new File(childPath);
if (filePath.exists() && filePath.isFile()) {
filePath.delete();
}
else if (filePath.exists() && filePath.isDirectory()) {
deleteFolder(filePath);
}
}
folder.delete();
}Top
3 楼nimifeng(学海无涯.......苦作舟....理解是美!!!Mars.Neil)回复于 2005-04-12 09:46:49 得分 0
如上...Top
4 楼ChDw(米)回复于 2005-04-12 09:49:48 得分 0
应该使用 folder.listFiles(); 更方便,不必须在下面自己再去创建File对象Top
5 楼wu_lin_326()回复于 2005-04-12 10:09:36 得分 0
如何把这个递归写到我得jsp页面中牙? 小弟初学 如我由d:/lin lin是个文件夹
<% ???? %>Top
6 楼ChDw(米)回复于 2005-04-12 10:18:38 得分 0
记得这个!号。
<%!
public void del() {
...
}
%>
Top
7 楼wu_lin_326()回复于 2005-04-12 10:52:36 得分 0
<%!
String path="D:/lin";
File filepath=new File(path);
private void deleteFolder(File folder) {
String childs[] = folder.list();
if (childs == null || childs.length <= 0) {
folder.delete();
}
for (int i = 0; i < childs.length; i++) {
String childName = childs[i];
String childPath = folder.getPath() + File.separator + childName;
File filePath = new File(childPath);
if (filePath.exists() && filePath.isFile()) {
filePath.delete();
}
else if (filePath.exists() && filePath.isDirectory()) {
deleteFolder(filePath);
}
}
folder.delete();
}
%>
这么写对吗? 为什么删不掉呢? 呜呜Top
8 楼wu_lin_326()回复于 2005-04-12 11:28:07 得分 0
??????Top
9 楼wu_lin_326()回复于 2005-04-12 15:31:32 得分 0
?Top
10 楼jebit(Jebit)回复于 2005-04-12 18:39:48 得分 0
可以调用系统的命令,不过就系统相关了,用
runtime.exec("deltree dir");Top
11 楼wu_lin_326()回复于 2005-04-13 08:43:02 得分 0
??Top
12 楼wu_lin_326()回复于 2005-04-13 08:44:19 得分 0
如何用jsp做到文件夹及子文件删除? 没人会吗Top




