如何在Java中删除包含其他文件夹的文件夹?
发布时间:2020-12-15 04:42:03 所属栏目:Java 来源:网络整理
导读:参见英文答案 Delete directories recursively in Java????????????????????????????????????23个 这是我试过的代码: import java.io.*;public class file03 { public static void main(String[] args) { File f1 = new File("C:/tempo1/tempo"); f1.mkdirs
|
参见英文答案 >
Delete directories recursively in Java????????????????????????????????????23个
这是我试过的代码: import java.io.*;
public class file03 {
public static void main(String[] args) {
File f1 = new File("C:/tempo1/tempo");
f1.mkdirs();
File f2 = new File("C:/test");
if(!f2.exists()) {
f2.mkdir();
}
f1 = new File("C:/tempo1/kempo");
f1.mkdirs();
f1 = new File("C:/tempo1");
String[] t = {};
if(f1.exists()) {
t = f1.list();
System.out.println(t.length + " files found");
}
for(int i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
try {
Thread.sleep(3000);
}
catch(Exception e) {}
f2.delete();
f2 = new File("C:/tempo1/test.txt");
try {
f2.createNewFile();
}
catch(Exception e) {}
try {
Thread.sleep(7000);
}
catch(Exception e) {}
File f3 = new File("C:/tempo1/renametesting.txt");
f2.renameTo(f3);
try {
Thread.sleep(5000);
}
catch(Exception e) {}
f3 = new File("C:/tempo1");
f3.delete();
}
}
我注意到的是,当文件夹测试被删除时,文件夹tempo1不会被删除.是因为它包含其他文件夹和文件吗?如果是这样,我该如何删除它? 解决方法
要删除包含文件的文件夹,不需要循环或递归搜索.你可以直接使用:
FileUtils.deleteDirectory(<File object of directory>); 此功能将删除文件夹及其中的所有文件 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 聊聊Java内存模型
- java – SWT DateTime格式更改
- 14-02 Java Math类,Random类,System类,BigDecimal类
- java – Spring数据 – MongoDb查找路线周围的最近位置
- availableProcessors is already set to [8], rejecting [8
- 请别再拿“String s = new String("xyz");创建了多
- 再次使用java中的字符串比较
- java – 如何将Object与Thread链接,以便wait()和notify()工
- 计算JAVA源代码行数的代码
- Java – EJB3带来了哪些功能,以及EJB3如何与Spring Framewo
推荐文章
站长推荐
热点阅读
