title: Writing a formatted string to a file - Java
body: <p>I have a string that I format with the <code>System.out.format()</code> method, I do something like :</p>
  
  <pre><code>System.out.format("I = %3d  var = %9.6f", i, myVar);
  </code></pre>
  
  <p>but when I try to write this formatted string into a file, I only get something like <code>"java.io.PrintStream@84fc8d"</code> in it. </p>
  
  <p>After looking into the documentation understood that this method is a little like <code>System.out.print()</code> and just return a PrintStream to display (in console for example), so I tried converting it with <code>.toString</code> or <code>String.valueOf()</code> but I get the same result.</p>
  
  <p>So I was wondering if there is a way to format a string just like the <code>String.out.format()</code> method does, but in a way that will be writable in a file ?</p>
  
  <p>Here is approximately the code I use (just making the useful parts appear)</p>
  
  <pre><code>WRITE_MY_LINE(System.out.format(" I = %3d  var = %9.6f", i, myVar).toString());
  //also tried this :
  WRITE_MY_LINE(String.valueOf(System.out.format(" I = %3d  var = %9.6f", i, myVar)));
  
  public static void WRITE_MY_LINE(String line) {
          buff_out = new BufferedWriter(new FileWriter(ascii_path, true));
  
          buff_out.append(line);
          buff_out.newLine();
          buff_out.flush();
  }
  </code></pre>