SHOW:
|
|
- or go back to the newest paste.
| 1 | package task; | |
| 2 | ||
| 3 | import java.util.concurrent.*; | |
| 4 | import java.util.*; | |
| 5 | import java.io.*; | |
| 6 | ||
| 7 | class FolderScan implements Runnable {
| |
| 8 | ||
| 9 | private String path; | |
| 10 | private BlockingQueue<File> queue; | |
| 11 | private CountDownLatch latch; | |
| 12 | private File endOfWorkFile; | |
| 13 | ||
| 14 | FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch, | |
| 15 | File endOfWorkFile) {
| |
| 16 | this.path = path; | |
| 17 | this.queue = queue; | |
| 18 | this.latch = latch; | |
| 19 | this.endOfWorkFile = endOfWorkFile; | |
| 20 | } | |
| 21 | ||
| 22 | - | public FolderScan() { }
|
| 22 | + | public FolderScan() {
|
| 23 | } | |
| 24 | ||
| 25 | @Override | |
| 26 | public void run() {
| |
| 27 | findFiles(path); | |
| 28 | queue.add(endOfWorkFile); | |
| 29 | latch.countDown(); | |
| 30 | } | |
| 31 | ||
| 32 | private void findFiles(String path) {
| |
| 33 | ||
| 34 | try {
| |
| 35 | File root = new File(path); | |
| 36 | File[] list = root.listFiles(); | |
| 37 | for (File currentFile : list) {
| |
| 38 | if (currentFile.isDirectory()) {
| |
| 39 | findFiles(currentFile.getAbsolutePath()); | |
| 40 | } else {
| |
| 41 | - | queue.put(currentFile); |
| 41 | + | |
| 42 | queue.put(currentFile); | |
| 43 | } | |
| 44 | } | |
| 45 | } | |
| 46 | } catch (InterruptedException e) {
| |
| 47 | e.printStackTrace(); | |
| 48 | } | |
| 49 | ||
| 50 | } | |
| 51 | ||
| 52 | } | |
| 53 | ||
| 54 | public class FileScan implements Runnable {
| |
| 55 | ||
| 56 | private String whatFind; | |
| 57 | private BlockingQueue<File> queue; | |
| 58 | private CountDownLatch latch; | |
| 59 | private File endOfWorkFile; | |
| 60 | ||
| 61 | public FileScan(String whatFind, BlockingQueue<File> queue, | |
| 62 | CountDownLatch latch, File endOfWorkFile) {
| |
| 63 | this.whatFind = whatFind; | |
| 64 | this.queue = queue; | |
| 65 | this.latch = latch; | |
| 66 | this.endOfWorkFile = endOfWorkFile; | |
| 67 | } | |
| 68 | - | public FileScan() { }
|
| 68 | + | |
| 69 | public FileScan() {
| |
| 70 | - | Set<String> words = new HashSet<String>(); |
| 70 | + | |
| 71 | - | int matches = 0; |
| 71 | + | |
| 72 | @Override | |
| 73 | public void run() {
| |
| 74 | ||
| 75 | while (true) {
| |
| 76 | try {
| |
| 77 | File file; | |
| 78 | file = queue.take(); | |
| 79 | ||
| 80 | if (file == endOfWorkFile) {
| |
| 81 | break; | |
| 82 | } | |
| 83 | ||
| 84 | scan(file); | |
| 85 | } catch (InterruptedException e) {
| |
| 86 | e.printStackTrace(); | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | latch.countDown(); | |
| 91 | } | |
| 92 | ||
| 93 | private void scan(File file) {
| |
| 94 | Scanner scanner = null; | |
| 95 | int matches = 0; | |
| 96 | ||
| 97 | try {
| |
| 98 | scanner = new Scanner(file); | |
| 99 | - | System.out.println("FileNotFoundException.");
|
| 99 | + | |
| 100 | System.out.println("File Not Found.");
| |
| 101 | e.printStackTrace(); | |
| 102 | - | while (scanner.hasNext()) {
|
| 102 | + | |
| 103 | - | String word = scanner.next(); |
| 103 | + | |
| 104 | - | words.add(word); |
| 104 | + | while (scanner.hasNext()) |
| 105 | if (scanner.next().equals(whatFind)) {
| |
| 106 | matches++; | |
| 107 | - | if (words.contains(this.whatFind)) {
|
| 107 | + | |
| 108 | - | matches++; |
| 108 | + | |
| 109 | if (matches > 0) {
| |
| 110 | - | |
| 110 | + | String myStr = String.format( |
| 111 | - | String myStr = String.format("File: %s and the number of matches "
|
| 111 | + | "File: %s - and the number of matches " + "is: %d", |
| 112 | - | + "is = %d", file.getAbsolutePath(), matches); |
| 112 | + | file.getAbsolutePath(), matches); |
| 113 | - | System.out.println(myStr); |
| 113 | + | System.out.println(myStr); |
| 114 | } | |
| 115 | - | matches = 0; |
| 115 | + | |
| 116 | ||
| 117 | // ask user about input | |
| 118 | public void askUserPathAndWord() {
| |
| 119 | ||
| 120 | BufferedReader bufferedReader = new BufferedReader( | |
| 121 | new InputStreamReader(System.in)); | |
| 122 | String path; | |
| 123 | String whatFind; | |
| 124 | BlockingQueue<File> queue = new LinkedBlockingQueue<File>(); | |
| 125 | ||
| 126 | try {
| |
| 127 | System.out.println("Please, enter a Path and Word"
| |
| 128 | + "(which you want to find):"); | |
| 129 | System.out.println("Please enter a Path:");
| |
| 130 | path = bufferedReader.readLine(); | |
| 131 | System.out.println("Please enter a Word:");
| |
| 132 | whatFind = bufferedReader.readLine(); | |
| 133 | ||
| 134 | if (path != null && whatFind != null) {
| |
| 135 | ||
| 136 | File endOfWorkFile = new File("GameOver.tmp");
| |
| 137 | CountDownLatch latch = new CountDownLatch(2); | |
| 138 | ||
| 139 | FolderScan folderScan = new FolderScan(path, queue, latch, | |
| 140 | endOfWorkFile); | |
| 141 | FileScan fileScan = new FileScan(whatFind, queue, latch, | |
| 142 | endOfWorkFile); | |
| 143 | ||
| 144 | Executor executor = Executors.newCachedThreadPool(); | |
| 145 | executor.execute(folderScan); | |
| 146 | executor.execute(fileScan); | |
| 147 | ||
| 148 | latch.await(); | |
| 149 | System.out.println("Thank you!");
| |
| 150 | } else {
| |
| 151 | System.out.println("You did not enter anything");
| |
| 152 | } | |
| 153 | ||
| 154 | } catch (IOException | RuntimeException e) {
| |
| 155 | System.out.println("Wrong input!");
| |
| 156 | e.printStackTrace(); | |
| 157 | } catch (InterruptedException e) {
| |
| 158 | System.out.println("Interrupted.");
| |
| 159 | e.printStackTrace(); | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * @param args | |
| 165 | */ | |
| 166 | ||
| 167 | public static void main(String[] args) {
| |
| 168 | long startTime = System.currentTimeMillis(); | |
| 169 | ||
| 170 | new FileScan().askUserPathAndWord(); | |
| 171 | ||
| 172 | long stopTime = System.currentTimeMillis(); | |
| 173 | long elapsedTime = stopTime - startTime; | |
| 174 | System.out.println("\nRuntime time " + elapsedTime + " milliseconds.");
| |
| 175 | } | |
| 176 | } |