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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 丁岩 中级黑马   /  2012-9-8 16:40  /  1852 人查看  /  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 个回复

倒序浏览
沙发!虽然不懂这个,不过好像根目录只能是C:\    D:\这样的格式吧?
回复 使用道具 举报

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();
if (files != null) {      //这里需要一个判断,否则如果dir是个空目录,则返回null,files就为空,程序到下面files.length就会出错
   for (int i = 0; i < files.length; i++)
   {
    if (files.isDirectory())
     basic(files, x, msg);
    else
     switch (x)
     {
     case 1:
      System.out.println(files);
      break;
     case 2:
      delete_1(files, msg);
      break;
     case 3:
      delete_2(files, msg);
      break;
     case 4:
      find_1(files, msg);
      break;
     case 5:
      find_2(files, 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;
}

评分

参与人数 1技术分 +1 收起 理由
田建 + 1

查看全部评分

回复 使用道具 举报
你看看我的博客,这就是答案
http://blog.csdn.net/alai_5186_cdsn/article/details/7948104
我运行你的代码没有报错。是因为我的C盘不是空的,有文件、
回复 使用道具 举报
杨震 发表于 2012-9-8 20:58
public static void main(String[] args) throws IOException
{
  System.out.println("请输入要操作的 ...

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