title: How can I write characters to a file in Java?
body: <p>I want to write some characters to a file. For the purpose I wrote the following code:</p>
  
  <pre><code>import java.io.*;
  
  public class write_char {
      public static void main(String[] args) throws IOException{
          String name = "shahjahan.txt";
  
          PrintWriter outFile = new PrintWriter(new FileWriter(name));
          String text = " Hello, how are you?";
  
          for ( int i = 0 ; i &lt; text.length() ; i++ )
          {
              outFile.write(text.charAt(i));
              outFile.write("\n");
              }
      }
  }
  </code></pre>
  
  <p>But the file shahjahan.text is empty. I am novice to java. Please help me.</p>
  
  <p>Is it necessary to close the file using close or flush. In C we donot require to close it.</p>