- package days15;
- import java.io.*;
- import java.util.*;
- import java.util.Scanner;
- public class Remove
- {
- public static void main(String[]args) throws IOException
- {
- System.out.println("请输入要操作的路径:");
- Scanner scr=new Scanner(System.in);
- String str=scr.next();
- File dir=new File(str);//指定文件路径
- System.out.println(" 1、指定范围文件遍历 2、删除包含该关键字的文件 3、删除以该关键字结尾的文件 4、查找包含该关键字的文件 5、查找以该关键字结尾的文件");
- int x=scr.nextInt();
- String msg=null;
- if(x!=1)
- {
- msg=imp();
- }
- long start=System.currentTimeMillis();
- basic(dir,x,msg);
- open(x);
- long end=System.currentTimeMillis();
- long time=end-start;
- if(time<=10000)
- System.out.println("本次执行共花费"+time+"毫秒");
- else
- if(time>10000&&time<=100000)
- System.out.println("本次执行共花费"+time/1000+"秒");
- }
- //1、文件遍历 2、删除包含该关键字 3、删除以该关键字结尾 4、查找包含该关键字 5、查找以该关键字结尾
- public static int basic(File dir,int x,String msg) throws IOException
- {
- File[]files=dir.listFiles();
- for(int i=0;i<files.length;i++)
- {
- if(files[i].isDirectory())
- basic(files[i],x,msg);
- else
- switch(x)
- {
- case 1:System.out.println(files[i]);break;
- case 2:delete_1(files[i],msg);break;
- case 3:delete_2(files[i],msg);break;
- case 4:find_1(files[i],msg);break;
- case 5:find_2(files[i],msg);break;
- }
- }
- return x;
- }
- public static void delete_1(File dir,String msg) throws IOException//2号功能
- {
- if(dir.toString().contains(msg))//获取文件名,包含关键字的删除
- {
- System.out.println(dir+"---------------删除成功");
- dir.delete();
- }
- }
- public static void delete_2(File dir,String msg) throws IOException //3号功能
- {
- if(dir.toString().endsWith(msg))//获取文件名,以该关键字结尾的删除
- {
- System.out.println(dir+"-----------------删除成功");
- dir.delete();
- }
- }
- public static void find_1(File dir,String msg) throws IOException//4号功能
- {
- boolean flag=true;
- if(dir.toString().contains(msg))//获取文件名,包含关键字的查找
- {
- System.out.println(dir+"------------查找成功");
- flag=false;
- }
- }
- public static void find_2(File dir,String msg) throws IOException//5号功能
- {
- boolean flag=true;
- if(dir.toString().endsWith(msg))//获取文件名,包含关键字的查找
- {
- System.out.println(dir+"---------------查找成功");
- flag=false;
- }
- }
- public static void open(int x) throws IOException //打开程序
- {
- if(x!=1)
- {
- Runtime r=Runtime.getRuntime();
- r.exec("cmd /k start D:\\test.txt");//打开程序
- }
- }
- public static String imp() throws IOException
- {
- System.out.println("请输入关键字信息:"); //关键字
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- String str = br.readLine();
- return str;
- }
- }
复制代码 只要是路径设置成C:\\、D:\\这样的根路径,总会报java.lang.NullPointerException,其他的子目录都没有事。。。这是为什么呢?头都大了~~~~~~~~~
|
|