当前位置:   article > 正文

使用Callable ,Future实现查找指定目录以及子目录下的指定关键字_callable放在哪里目录

callable放在哪里目录

在给定的某个文件夹下查找给定的关键字keywords

程序源于Java核心技术(卷I)第8版 基础知识(中文版) 

作者:Cay S.Horstmann,Gray Cornell ,

第14章多线程中的第14.8节Callable ,Future,源码如下:


  1. package v1ch14.FutureTest;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.concurrent.*;
  5. /**
  6. * @version 1.0 2004-08-01
  7. * @author Cay Horstmann
  8. */
  9. public class FutureTest
  10. {
  11. public static void main(String[] args)
  12. {
  13. Scanner in = new Scanner(System.in);
  14. System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): ");
  15. String directory = in.nextLine();
  16. System.out.print("Enter keyword (e.g. volatile): ");
  17. String keyword = in.nextLine();
  18. MatchCounter counter = new MatchCounter(new File(directory), keyword);
  19. FutureTask<Integer> task = new FutureTask<Integer>(counter);
  20. Thread t = new Thread(task);
  21. t.start();
  22. try
  23. {
  24. System.out.println(task.get() + " matching files.");
  25. }
  26. catch (ExecutionException e)
  27. {
  28. e.printStackTrace();
  29. }
  30. catch (InterruptedException e)
  31. {
  32. }
  33. }
  34. }
  35. /**
  36. * This task counts the files in a directory and its subdirectories that contain a given keyword.
  37. */
  38. class MatchCounter implements Callable<Integer>
  39. {
  40. /**
  41. * Constructs a MatchCounter.
  42. * @param directory the directory in which to start the search
  43. * @param keyword the keyword to look for
  44. */
  45. public MatchCounter(File directory, String keyword)
  46. {
  47. this.directory = directory;
  48. this.keyword = keyword;
  49. }
  50. public Integer call()
  51. {
  52. count = 0;
  53. try
  54. {
  55. File[] files = directory.listFiles();
  56. ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
  57. for (File file : files)
  58. if (file.isDirectory())
  59. {
  60. MatchCounter counter = new MatchCounter(file, keyword);
  61. FutureTask<Integer> task = new FutureTask<Integer>(counter);
  62. results.add(task);
  63. Thread t = new Thread(task);
  64. t.start();
  65. }
  66. else
  67. {
  68. if (search(file)) count++;
  69. }
  70. for (Future<Integer> result : results)
  71. try
  72. {
  73. count += result.get();
  74. }
  75. catch (ExecutionException e)
  76. {
  77. e.printStackTrace();
  78. }
  79. }
  80. catch (InterruptedException e)
  81. {
  82. }
  83. return count;
  84. }
  85. /**
  86. * Searches a file for a given keyword.
  87. * @param file the file to search
  88. * @return true if the keyword is contained in the file
  89. */
  90. public boolean search(File file)
  91. {
  92. try
  93. {
  94. Scanner in = new Scanner(new FileInputStream(file));
  95. boolean found = false;
  96. while (!found && in.hasNextLine())
  97. {
  98. String line = in.nextLine();
  99. if (line.contains(keyword)) found = true;
  100. }
  101. in.close();
  102. return found;
  103. }
  104. catch (IOException e)
  105. {
  106. return false;
  107. }
  108. }
  109. private File directory;
  110. private String keyword;
  111. private int count;
  112. }



本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/976209
推荐阅读
相关标签
  

闽ICP备14008679号