Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package com.mail.lenka949;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.Future;
  8. import java.util.concurrent.FutureTask;
  9.  
  10. public class FindFile {
  11. private String directory;
  12. private String file;
  13. private int threadNumber;
  14.  
  15. public FindFile() {
  16. super();
  17. }
  18.  
  19. public FindFile(String directory, String file) {
  20. super();
  21. this.directory = directory;
  22. this.file = file;
  23. this.threadNumber = 1;
  24. }
  25.  
  26. public FindFile(String directory, String file, int threadNumber) {
  27. super();
  28. this.directory = directory;
  29. this.file = file;
  30. this.threadNumber = threadNumber;
  31. }
  32.  
  33. public String getDirectory() {
  34. return directory;
  35. }
  36.  
  37. public void setDirectory(String directory) {
  38. this.directory = directory;
  39. }
  40.  
  41. public String getFile() {
  42. return file;
  43. }
  44.  
  45. public void setFile(String file) {
  46. this.file = file;
  47. }
  48.  
  49. private boolean isDirectoryExist() {
  50. File file = new File(directory);
  51. if (!file.exists()) {
  52. System.out.println("File source \"" + file.getName() + "\" is not a exists");
  53. return false;
  54. }
  55. if (!file.isDirectory()) {
  56. System.out.println("File source \"" + file.getName() + "\" is not a directory");
  57. return false;
  58. }
  59. return true;
  60. }
  61.  
  62. public void fileSearch() {
  63. if (!isDirectoryExist()) {
  64. return;
  65. }
  66. File fileDir = new File(directory);
  67. File[] filesList = getFilesList(fileDir);
  68. if (filesList.length == 0) {
  69. System.out.println("File source \"" + directory + "\" is empty");
  70. return;
  71. }
  72. ArrayList<Future<String>> result = new ArrayList<>();
  73.  
  74. int countTread = threadNumber;
  75. if (threadNumber > filesList.length) {
  76. countTread = filesList.length;
  77. }
  78. for (int i = 0; i < countTread; i++) {
  79. int size = filesList.length / countTread;
  80. int begin = size * i;
  81. int end = ((i + 1) * size);
  82. if ((filesList.length - end) < size) {
  83. end = filesList.length;
  84. }
  85. File[] arrayToSearch = Arrays.copyOfRange(filesList, begin, end);
  86. FutureTask<String> res = new FutureTask<>(new SearchThread(arrayToSearch, file));
  87. result.add(res);
  88. Thread thread = new Thread(res);
  89. thread.start();
  90. }
  91.  
  92. for (Future<String> future : result) {
  93. try {
  94. System.out.println(future.get());
  95. } catch (InterruptedException | ExecutionException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. System.out.println("Search completed");
  101. }
  102.  
  103. public File[] getFilesList(File directory) {
  104. File[] fileList = directory.listFiles();
  105. for (File file : fileList) {
  106. if (file.isDirectory()) {
  107. File[] filesDir = getFilesList(file);
  108. fileList = Arrays.copyOf(fileList, fileList.length + filesDir.length);
  109. System.arraycopy(filesDir, 0, fileList, fileList.length - filesDir.length, filesDir.length);
  110. }
  111. }
  112. return fileList;
  113. }
  114.  
  115. @Override
  116. public String toString() {
  117. return "FindFile [directory=" + directory + ", file=" + file + ", threadNumber=" + threadNumber + "]";
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement