title: Fastest way to load huge text file into a int array
body: <p>I have a big text file (+100MB), each line being an integer number (containing 10 million numbers). Of course, the size and amount may change, so I don't know this in advance.</p>
  
  <p>I want to load the file into a <code>int[]</code>, making the process as fast as posible. First I came to this solution:</p>
  
  <pre><code>public int[] fileToArray(String fileName) throws IOException
  {
      List&lt;String&gt; list = Files.readAllLines(Paths.get(fileName));
      int[] res = new int[list.size()];
      int pos = 0;
      for (String line: list)
      {
          res[pos++] = Integer.parseInt(line);
      }
      return res;
  }
  </code></pre>
  
  <p>It was pretty fast, 5.5 seconds. Of which, 5.1s goes for the <code>readAllLines</code> call, and 0.4s for the loop.</p>
  
  <p>But then I decided to try using BufferedReader, and came to this different solution:</p>
  
  <pre><code>public int[] fileToArray(String fileName) throws IOException
  {
      BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));
      ArrayList&lt;Integer&gt; ints = new ArrayList&lt;Integer&gt;();
      String line;
      while ((line = bufferedReader.readLine()) != null)
      {
          ints.add(Integer.parseInt(line));
      }
      bufferedReader.close();
  
      int[] res = new int[ints.size()];
      int pos = 0;
      for (Integer i: ints)
      {
          res[pos++] = i.intValue();
      }
      return res;
  }
  </code></pre>
  
  <p>This was even faster! 3.1 seconds, just 3s for the <code>while</code> loop and not even 0.1s for the <code>for</code> loop.</p>
  
  <p>I know there is no much space here for optimization, at least in time, but using an ArrayList and then a int[] seems like too much memory to me.</p>
  
  <p>Any ideas on how to make this faster, or avoid using the middle ArrayList?</p>
  
  <p>Just for comparison, I do this same task with FreePascal in 1.9 seconds [see edit], using <code>TStringList</code> class and <code>StrToInt</code> function.</p>
  
  <p><strong>EDIT</strong>: Since I got a pretty short time with Java method, I had to improve the FreePascal one. 330~360ms.</p>