【发布时间】:2014-02-15 12:13:41
【问题描述】:
这是我试过的代码:
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();
}
}
我注意到,当文件夹 test 被删除时,文件夹 tempo1 并没有被删除。是因为它包含其他文件夹和文件吗?如果是这样,我该如何删除它? 我正在使用 BlueJ IDE。
【问题讨论】:
-
我认为你必须递归地这样做
-
您将不得不递归删除文件和目录。
-
但是没有其他方法可以直接删除吗?删除递归听起来很费力。
-
以下帖子应该会有所帮助。它与您的相似:[stackoverflow.com/questions/779519/… [1]:stackoverflow.com/questions/779519/…
标签: java file file-io directory bluej