title: Fastest method for reading file data in Arrays (Java)
body: <p>Take a look at the following link:</p>
  
  <p><a href="http://snippetsofjosh.wordpress.com/tag/advantages-and-disadvantages-of-arraylist/" rel="nofollow noreferrer">http://snippetsofjosh.wordpress.com/tag/advantages-and-disadvantages-of-arraylist/</a></p>
  
  <p>This one of the reasons why I always prefer to use Arrays instead of (Array)Lists. 
  Still, this got me thinking about memory management and speed.</p>
  
  <p>Hence I arrived at the following question: </p>
  
  <p><strong>What is the best way to store data from a file when you don't know the size of the file (/number of entries)</strong> (where <em>best</em> is defined as '<em>the least amount of computation time</em>')</p>
  
  <p>Below, I will present 3 different methods and I would like to know which one of them is best and why. For the clarity of the question, let's assume I must end up with an Array. Also, let's assume every line from our .txt file only has one entry (/one string). Also, for limiting the scope of the questions, I will limit this question to Java only.</p>
  
  <p>Let's say we want to retrieve the following info from a file called <code>words.txt</code>:</p>
  
  <pre><code>Hello
  I 
  am
  a
  test 
  file
  </code></pre>
  
  <p><strong>Method 1 - Double and dangerous</strong></p>
  
  <pre><code>File read = new File("words.txt");
  Scanner in = new Scanner(read);
  
  int counter = 0;
  
  while (in.hasNextLine())
  {
      in.nextLine();
      counter++;
  }
  
  String[] data = new String[counter];
  
  in = new Scanner(read);
  
  int i = 0;
  
  while (in.hasNextLine())
  {
      data[i] = in.nextLine();
      i++;
  }
  </code></pre>
  
  <p><strong>Method 2 - Clear but redundant</strong></p>
  
  <pre><code>File read = new File("words.txt");
  Scanner in = new Scanner(read);
  
  ArrayList&lt;String&gt; temporary = new ArrayList&lt;String&gt;();
  
  while (in.hasNextLine())
  {
      temporary.add(in.nextLine());
  }
  
  String[] data = new String[temporary.size()];
  
  for (int i = 0; i &lt; temporary.size(); i++)
  {
      data[i] = temporary.get(i);
  }
  </code></pre>
  
  <p><strong>Method 3 - Short but rigid</strong></p>
  
  <pre><code>File read = new File("words.txt");
  FileReader reader = new FileReader(read);
  
  String content = null;
  
  char[] chars = new char[(int) read.length()];
  reader.read(chars);
  content = new String(chars);
  
  String[] data = content.split(System.getProperty("line.separator"));
  
  reader.close(); 
  </code></pre>
  
  <p>If you have an alternative way (which is even better) then please supply it below.
  Also, feel free to adjust my code where necessary.</p>
  
  <hr>
  
  <h2><a href="https://stackoverflow.com/a/17319712/2431885">Answer:</a></h2>
  
  <p>The fastest method for storing data in an array is the following method:</p>
  
  <pre><code>File read = new File("words.txt");
  Scanner in = new Scanner(read);
  
  ArrayList&lt;String&gt; temporary = new ArrayList&lt;String&gt;();
  
  while (in.hasNextLine()) {
      temporary.add(in.nextLine());
  }
  
  String[] data = temporary.toArray(new String[temporary.size()]);
  </code></pre>
  
  <p>And for Java 7+:</p>
  
  <pre><code>Path loc = Paths.get(URI.create("file:///Users/joe/FileTest.txt"));
  List&lt;String&gt; lines = Files.readAllLines(loc, Charset.defaultCharset());
  String[] array = lines.toArray(new String[lines.size()]);
  </code></pre>