title: How to read large text files faster in Java?
body: <p>I just made a simple sequential median filter algorithm and I happened to have used 2 scanners to handle command-line input and a scanner for reading the file. Now the problem is,I have a text file of <code>2,000,000</code> lines of 2 columns text in the form of <code>&lt;integer&gt; &lt;float&gt;</code> and it's taking a long time(more than 2 minutes) just to read the file.</p>
  
  <p>Basically the program simply grabs the input, uses the median filter algorithm and writes to an output file. </p>
  
  <p>Below is my code:
  <strong>main.java</strong></p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Scanner;
  
  public class main {
      public static void main(String args[]) throws NumberFormatException, IOException{
          String inFile; //Input file name.
          int filterSize; //Filter size (odd integer &gt;= 3).
          String outFile; //Output file name.
          int arraySize;
          List&lt;Float&gt; elements = new ArrayList&lt;Float&gt;();
          int index = 0;
  
          //Scanner to take input file name, filter size and output file name.
          Scanner keyboardInput = new Scanner(System.in);
          System.out.println("Enter your keyboard input as follows: &lt;data file name&gt; &lt;filter size(odd int &gt;= 3&gt; &lt;output file name&gt;");
  
          //Assigning values to variables.
          inFile = keyboardInput.next();
          filterSize = keyboardInput.nextInt();
          outFile = keyboardInput.next();
  
  
  //      //Reading file
  //      Scanner readFile = new Scanner(new File(inFile));
  //      readFile.nextInt(); //Get Array Size        
  //      
  //      //Add elements into ArrayList
  //      while(readFile.hasNext()){
  //          readFile.nextInt();
  //          elements.add(Float.parseFloat(readFile.next()));
  //      }    
  
          //Reading file
          BufferedReader br = new BufferedReader(new FileReader(inFile));
          br.readLine(); //Get Array Size 
  
          String line;
          while((line = br.readLine())!= null){
              String[] nums = line.split(" ");
              int val = Integer.valueOf(nums[0]);
              elements.add(Float.valueOf(nums[1]));
          }
          br.close();
  
          new Serial(elements, filterSize, outFile);
  
      }
  }
  </code></pre>
  
  <p><strong>Serial.java</strong></p>
  
  <pre><code>import java.io.FileNotFoundException;
  import java.io.PrintWriter;
  import java.io.UnsupportedEncodingException;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  
  public class Serial {
      int filterSize; //Filter size (odd integer &gt;= 3).
      String outFile; //Output file name.
      int arraySize;
      List&lt;Float&gt; elements = new ArrayList&lt;Float&gt;();
      int index = 0;
  
  
      public Serial(List&lt;Float&gt; elements, int filterSize, String outFile) throws FileNotFoundException, UnsupportedEncodingException {
          this.elements = elements;
          this.filterSize= filterSize;
          this.outFile = outFile;
  
  
          List&lt;Float&gt; tempElements = new ArrayList&lt;Float&gt;();
          List&lt;Float&gt; outputElements = new ArrayList&lt;Float&gt;();
  
          //Add first boundary element to ouput ArrayList
          outputElements.add(this.elements.get(0));
  
          while(elements.size() &gt;= filterSize){
              for(int i = 0; i&lt;filterSize; i++){
                  tempElements.add(this.elements.get(i));
              }
  
              Collections.sort(tempElements);
              outputElements.add(tempElements.get((filterSize-1)/2));
  
              elements.remove(0);
              tempElements.clear();
          }
  
          //Add last boundary element to output ArrayList
          if (elements != null &amp;&amp; !elements.isEmpty()) {
              outputElements.add(elements.get(elements.size()-1));
          }
  
          /*Trace. Checking if output is correct
          for(int i=0; i&lt;outputElements.size(); i++){
              System.out.println(outputElements.get(i));
          }*/
  
          //Write elements to output file     
          PrintWriter writeOutput = new PrintWriter(this.outFile, "UTF-8");
          writeOutput.println(outputElements.size());//Number of lines
          for(int i=0; i&lt;outputElements.size();i++){
              writeOutput.println(i+1 + " " + outputElements.get(i)); //Each line is written
          }
  
          writeOutput.close(); //Close when output finished writing.
      }   
  }
  </code></pre>
  
  <p>Is there a way to read the file faster(as in seconds)?</p>
  
  <p>Thanks</p>
  
  <p>Edit:
  Example input</p>
  
  <pre><code>5
  1 2
  2 80
  3 6
  4 3
  5 1
  </code></pre>