A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 丁岩 中级黑马   /  2012-9-8 16:40  /  1865 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package days15;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.Scanner;
  5. public class Remove
  6. {
  7. public static void main(String[]args) throws IOException
  8. {



  9. System.out.println("请输入要操作的路径:");

  10. Scanner scr=new Scanner(System.in);
  11. String str=scr.next();
  12. File dir=new File(str);//指定文件路径
  13. System.out.println(" 1、指定范围文件遍历 2、删除包含该关键字的文件 3、删除以该关键字结尾的文件 4、查找包含该关键字的文件 5、查找以该关键字结尾的文件");
  14. int x=scr.nextInt();
  15. String msg=null;
  16. if(x!=1)
  17. {
  18. msg=imp();
  19. }
  20. long start=System.currentTimeMillis();
  21. basic(dir,x,msg);
  22. open(x);
  23. long end=System.currentTimeMillis();
  24. long time=end-start;
  25. if(time<=10000)
  26. System.out.println("本次执行共花费"+time+"毫秒");
  27. else
  28. if(time>10000&&time<=100000)
  29. System.out.println("本次执行共花费"+time/1000+"秒");

  30. }

  31. //1、文件遍历 2、删除包含该关键字 3、删除以该关键字结尾 4、查找包含该关键字 5、查找以该关键字结尾
  32. public static int basic(File dir,int x,String msg) throws IOException
  33. {
  34. File[]files=dir.listFiles();
  35. for(int i=0;i<files.length;i++)
  36. {
  37. if(files[i].isDirectory())
  38. basic(files[i],x,msg);
  39. else
  40. switch(x)
  41. {
  42. case 1:System.out.println(files[i]);break;
  43. case 2:delete_1(files[i],msg);break;
  44. case 3:delete_2(files[i],msg);break;
  45. case 4:find_1(files[i],msg);break;
  46. case 5:find_2(files[i],msg);break;
  47. }

  48. }

  49. return x;
  50. }

  51. public static void delete_1(File dir,String msg) throws IOException//2号功能
  52. {
  53. if(dir.toString().contains(msg))//获取文件名,包含关键字的删除
  54. {
  55. System.out.println(dir+"---------------删除成功");
  56. dir.delete();
  57. }

  58. }

  59. public static void delete_2(File dir,String msg) throws IOException //3号功能
  60. {
  61. if(dir.toString().endsWith(msg))//获取文件名,以该关键字结尾的删除
  62. {
  63. System.out.println(dir+"-----------------删除成功");
  64. dir.delete();
  65. }

  66. }

  67. public static void find_1(File dir,String msg) throws IOException//4号功能
  68. {
  69. boolean flag=true;

  70. if(dir.toString().contains(msg))//获取文件名,包含关键字的查找
  71. {
  72. System.out.println(dir+"------------查找成功");
  73. flag=false;
  74. }


  75. }

  76. public static void find_2(File dir,String msg) throws IOException//5号功能
  77. {

  78. boolean flag=true;
  79. if(dir.toString().endsWith(msg))//获取文件名,包含关键字的查找
  80. {
  81. System.out.println(dir+"---------------查找成功");
  82. flag=false;
  83. }



  84. }
  85. public static void open(int x) throws IOException //打开程序
  86. {
  87. if(x!=1)
  88. {
  89. Runtime r=Runtime.getRuntime();

  90. r.exec("cmd /k start D:\\test.txt");//打开程序
  91. }
  92. }

  93. public static String imp() throws IOException
  94. {
  95. System.out.println("请输入关键字信息:"); //关键字
  96. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  97. String str = br.readLine();
  98. return str;

  99. }


  100. }
复制代码
只要是路径设置成C:\\、D:\\这样的根路径,总会报java.lang.NullPointerException,其他的子目录都没有事。。。这是为什么呢?头都大了~~~~~~~~~

4 个回复

倒序浏览
杨震 发表于 2012-9-8 20:58
public static void main(String[] args) throws IOException
{
  System.out.println("请输入要操作的 ...

大哥谢谢你!!!问题解决了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马