package shanchu;
import java.io.File;
import java.util.Scanner;
//键盘录入文件或目录的路径,完成删除操作
public class ShanChu {
public static void main(String[] args) {
System.out.println("请输入要删除文件或目录的路径:");
Scanner sc = new Scanner(System.in);
String src = sc.nextLine();
File srcFile = new File(src);
//调用deleteFile() 方法 完成删除操作
deleteFile(srcFile);
System.out.println("删除完毕!");
}
private static void deleteFile(File srcFile) {
if(srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
for (File f : files) {
deleteFile(f);
}
srcFile.delete();
}
else{
srcFile.delete();
}
}
}
|
|