这里是我试过代码:如何删除包含Java中其他文件夹的文件夹?
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没有被删除。是否因为它包含其他文件夹和文件?如果是这样,我该如何删除它? 我正在使用BlueJ IDE。
我认为你必须这样做递归 – BackSlash
你将不得不递归地删除文件和目录。 – Rahul
但是没有其他方法可以直接删除它吗?删除递归声音相当费力。 – user3177527