Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.rs.worldserver.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.nio.file.*;
  7. import java.nio.file.attribute.BasicFileAttributes;
  8. import java.util.ArrayDeque;
  9.  
  10. /**
  11.  * @author Stan
  12.  */
  13.  
  14. public class Scanner {
  15.  
  16.     public static final String CHARACTER_FILE_PATH = "./savedgames/";
  17.    
  18.     public static void main(final String... args) throws IOException {
  19.         search();
  20.     }
  21.    
  22.     public static void search() throws IOException{
  23.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  24.        
  25.         System.out.print("Enter your desired keyword: ");
  26.  
  27.         String keyword = reader.readLine();
  28.  
  29.         System.out.println("Started scanning all files...");
  30.         long start = System.currentTimeMillis();
  31.         try {
  32.             Path path = Paths.get(CHARACTER_FILE_PATH);
  33.             Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
  34.                 @Override
  35.                 public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
  36.                     System.out.print(path);
  37.                     if(new String(Files.readAllBytes(path)).contains(keyword))
  38.                         matches.add(path.toString());
  39.                     return FileVisitResult.CONTINUE;
  40.                 }
  41.             });
  42.         } catch (IOException e) {
  43.             System.out.println("Make sure this is in the same directory as "+CHARACTER_FILE_PATH);
  44.         }
  45.         long duration = System.currentTimeMillis() - start;
  46.         System.out.println("Found "+matches.size()+" matches for the keyword "+keyword);
  47.         matches.forEach(System.out::println);
  48.         System.out.println("The scan toke " + duration + "ms to complete.");
  49.         System.out.print("For another scan type -r: ");
  50.         matches.clear();
  51.        
  52.         if(reader.readLine().equalsIgnoreCase("-r"))
  53.             search();
  54.         reader.close();
  55.     }
  56.    
  57.     private static ArrayDeque<String> matches = new ArrayDeque<String>(200);
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement