title: The fastest way to save large data to a file
body: <p>I do some numerical calculation in Java, C# and C++. Some of them save a lot of data (to the text file). What is the fastest way to do it?</p>
  
  <p><strong>C++</strong>. </p>
  
  <pre><code>ofstream file;
  file.open(plik);
  for(int i=0;i&lt;251;i++){
      for(int j=0;j&lt;81;j++)
          file&lt;&lt;(i-100)*0.01&lt;&lt;" "&lt;&lt;(j-40)*0.01&lt;&lt;" "&lt;&lt;U[i][j]&lt;&lt;endl;
      file&lt;&lt;endl;
  }
  </code></pre>
  
  <p>Which I assume is very fast ( am I right?:) )</p>
  
  <p><strong>Java</strong></p>
  
  <pre><code>void SaveOutput(double[][] U, String fileName) throws IOException
  {
      PrintWriter tx = new PrintWriter(new FileWriter(fileName));
      for(int i=0;i&lt;251;i++)
      {
          for(int j=0;j&lt;81;j++)
          {
              tx.println(String.format("%e %e %e ",(i - 100) * dz, (j - 40) * dz, U[i][j]));
          }
          tx.println();
      }
      tx.close();
  }
  </code></pre>
  
  <p>The C# example is similar.</p>
  
  <p>and here is what bothers me. I make a String object for each line (a lot of garbage). In this example it is not that much but sometimes I have 10 000 000 lines. This leads me to questions:</p>
  
  <ol>
  <li>Can the c++ example be faster?</li>
  <li>Should I use StringBuilder for Java or maybe it also bad due to number of lines</li>
  <li>There is any other way or library?</li>
  <li>What about C#?</li>
  </ol>
  
  <p>Thank you</p>