title: How to quickly search a large file for a String in Java?
body: <p>I am trying to search a large text file (400MB) for a particular String using the following:</p>
  
  <pre><code>File file = new File("fileName.txt");
  try {
      int count = 0;
      Scanner scanner = new Scanner(file);
      while(scanner.hasNextLine()) {
          if(scanner.nextLine().contains("particularString")) {
              count++;
              System.out.println("Number of instances of String: " + count);
          }
      }
  } catch (FileNotFoundException e){
      System.out.println(e);
  }
  </code></pre>
  
  <p>This works fine for small files however for this particular file and other large ones it takes far too long (>10mins).</p>
  
  <p>What would be the quickest, most efficient way of doing this?</p>
  
  <p>I have now changed to the following and it completes within seconds - </p>
  
  <pre><code>try {
          int count = 0;
          FileReader fileIn = new FileReader(file);
          BufferedReader reader = new BufferedReader(fileIn);
          String line;
          while((line = reader.readLine()) != null) {
              if((line.contains("particularString"))) {
                  count++;
                  System.out.println("Number of instances of String " + count);
              }
          }
      }catch (IOException e){
          System.out.println(e);
      }
  </code></pre>