Advertisement
nazar_art

FileScan

Feb 22nd, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  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() {  }
  23.  
  24.     @Override
  25.     public void run() {
  26.         findFiles(path);
  27.         queue.add(endOfWorkFile);
  28.         latch.countDown();
  29.     }
  30.  
  31.     private void findFiles(String path) {
  32.  
  33.         try {
  34.             File root = new File(path);
  35.             File[] list = root.listFiles();
  36.             for (File currentFile : list) {
  37.                 if (currentFile.isDirectory()) {
  38.                     findFiles(currentFile.getAbsolutePath());
  39.                 } else {
  40.                     if (currentFile.getName().toLowerCase().endsWith((".txt"))) {
  41.                             queue.put(currentFile);
  42.                     }
  43.                 }
  44.             }
  45.         } catch (InterruptedException e) {
  46.             e.printStackTrace();
  47.         }
  48.  
  49.     }
  50.  
  51. }
  52.  
  53. public class FileScan implements Runnable {
  54.  
  55.     private String whatFind;
  56.     private BlockingQueue<File> queue;
  57.     private CountDownLatch latch;
  58.     private File endOfWorkFile;
  59.  
  60.     public FileScan(String whatFind, BlockingQueue<File> queue,
  61.             CountDownLatch latch, File endOfWorkFile) {
  62.         this.whatFind = whatFind;
  63.         this.queue = queue;
  64.         this.latch = latch;
  65.         this.endOfWorkFile = endOfWorkFile;
  66.     }
  67.  
  68.     public FileScan() {     }
  69.  
  70.     Set<String> words = new HashSet<String>();
  71.     int matches = 0;
  72.  
  73.     @Override
  74.     public void run() {
  75.  
  76.         while (true) {
  77.             try {
  78.                 File file;
  79.                 file = queue.take();
  80.  
  81.                 if (file == endOfWorkFile) {
  82.                     break;
  83.                 }
  84.  
  85.                 scan(file);
  86.             } catch (InterruptedException e) {
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.  
  91.         latch.countDown();
  92.     }
  93.  
  94.     private void scan(File file) {
  95.         Scanner scanner = null;
  96.         try {
  97.             scanner = new Scanner(file);
  98.         } catch (FileNotFoundException e) {
  99.             System.out.println("FileNotFoundException.");
  100.             e.printStackTrace();
  101.         }
  102.         while (scanner.hasNext()) {
  103.             String word = scanner.next();
  104.             words.add(word);
  105.         }
  106.  
  107.         if (words.contains(this.whatFind)) {
  108.             matches++;
  109.         }
  110.        
  111.         String myStr = String.format("File: %s and the number of matches "
  112.                 + "is = %d", file.getAbsolutePath(), matches);
  113.         System.out.println(myStr);
  114.  
  115.         matches = 0;
  116.     }
  117.  
  118.     // ask user about input
  119.     public void askUserPathAndWord() {
  120.  
  121.         BufferedReader bufferedReader = new BufferedReader(
  122.                 new InputStreamReader(System.in));
  123.         String path;
  124.         String whatFind;
  125.         BlockingQueue<File> queue = new LinkedBlockingQueue<File>();
  126.  
  127.         try {
  128.             System.out.println("Please, enter a Path and Word"
  129.                     + "(which you want to find):");
  130.             System.out.println("Please enter a Path:");
  131.             path = bufferedReader.readLine();
  132.             System.out.println("Please enter a Word:");
  133.             whatFind = bufferedReader.readLine();
  134.  
  135.             if (path != null && whatFind != null) {
  136.  
  137.                 File endOfWorkFile = new File("GameOver.tmp");
  138.                 CountDownLatch latch = new CountDownLatch(2);
  139.  
  140.                 FolderScan folderScan = new FolderScan(path, queue, latch,
  141.                         endOfWorkFile);
  142.                 FileScan fileScan = new FileScan(whatFind, queue, latch,
  143.                         endOfWorkFile);
  144.  
  145.                 Executor executor = Executors.newCachedThreadPool();
  146.                 executor.execute(folderScan);
  147.                 executor.execute(fileScan);
  148.  
  149.                 latch.await();
  150.                 System.out.println("Thank you!");
  151.             } else {
  152.                 System.out.println("You did not enter anything");
  153.             }
  154.  
  155.         } catch (IOException | RuntimeException e) {
  156.             System.out.println("Wrong input!");
  157.             e.printStackTrace();
  158.         } catch (InterruptedException e) {
  159.             System.out.println("Interrupted.");
  160.             e.printStackTrace();
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * @param args
  166.      */
  167.  
  168.     public static void main(String[] args) {
  169.         new FileScan().askUserPathAndWord();
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement