1 /*
2 删除目录下所有的.tpm & .bak文件
3 */
4 import java.io.*;
5 public class DelBakTmp
6 {
7 public static void main(String[] args){
8 //提示
9 System.out.println("本程序用于删除目录下的所有.tmp和.bak文件");
10 System.out.println("┏━━━━━━━━━━━━━━━━━━━┓");
11 System.out.println("┃!!!!不走回收站!!谨慎选择!!!!┃");
12 System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
13 System.out.println("请输入要检查的路径名称:");
14
15 String filePath = getPath();
16
17 checkFile(filePath);
18 }
19
20 //判断路径
21 public static void checkFile(String filePath){
22 if(filePath!=null){
23 filePath = filePath.replaceAll("\\\\","\\\\\\\\");
24 File file = new File(filePath);
25 if(file.exists() && file.isDirectory()){
26 traverseFile(file);
27 System.out.println("完成!!!");
28 }
29 else{
30 System.out.println("不是目录或目录不存在,请重新输入:");
31 checkFile(getPath());
32 }
33 }
34 }
35
36 //获取输入的路径
37 public static String getPath(){
38 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
39 try{
40 return bufr.readLine();
41 }catch(IOException e){
42 System.out.println("读取文件失败");
43 }
44 return null;
45 }
46
47 //递归读取所有文件及文件夹
48 public static void traverseFile(File file) {
49 if(!(file.isHidden())) {
50 File[] files = file.listFiles();
51 if(!(files ==null)){
52 for(File f : files){
53 if(f.isDirectory())
54 traverseFile(f);
55 else
56 delFile(f);
57 }
58 }
59 }
60 }
61
62 //将符合条件的文件删除
63 public static void delFile(File file) {
64 if(file.getName().endsWith(".tmp") || file.getName().endsWith(".bak")){
65 boolean b = file.delete();
66 System.out.println(file.getAbsolutePath()+" 删除:"+ (b?"成功":"失败"));
67 }
68 }
69 }