package com.practice_25;
import java.util.Scanner;
import java.io.File;
public class day21deleteFile {
public static void main(String agrs[]) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter a file path");
File file = new File(sc.nextLine());
boolean flag = deleteFile(file);
System.out.println("flag=" + flag);
}
public static boolean deleteFile(File file) {
boolean flag = false;
System.out.println(file.getName() + "is File" + file.isFile()
+ " is directory" + file.isDirectory());
if (file.isFile()) {
flag = file.delete();
} else if (file.isDirectory()) {
for (File file1 : file.listFiles()) {
flag = deleteFile(file1);
}
file.deleteOnExit();
} else {
System.out.println("this is not a file");
}
return flag;
}
}
|
|