title: How to write new line character to a file in Java
body: <p>I have a string that contains new lines. I send this string to a function to write the String to a text file as:</p>
  
  <pre><code>    public static void writeResult(String writeFileName, String text)
      {
          try
          {
          FileWriter fileWriter = new FileWriter(writeFileName);
          BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  
          bufferedWriter.write(text);
  
          // Always close files.
          bufferedWriter.close();
  
          }
          catch(IOException ex) {
              System.out.println("Error writing to file '"+ writeFileName + "'");}
      } //end writeResult function
  </code></pre>
  
  <p>But when I open the file, I find it without any new lines.
  When I display the text in the console screen, it is displayed with new lines. How can I write the new line character in the text file.</p>
  
  <p><strong>EDIT:</strong>
  Assume this is the argument <code>text</code> that I sent to the function above:</p>
  
  <pre><code>I returned from the city about three o'clock on that
  may afternoon pretty well disgusted with life.
  I had been three months in the old country, and was
  </code></pre>
  
  <p>How to write this string as it is (with new lines) in the text file. My function write the string in one line. Can you provide me with a way to write the text to the file including new lines ?</p>
  
  <p><strong>EDIT 2:</strong>
  The text is originally in a .txt file. I read the text using:</p>
  
  <pre><code>while((line = bufferedReader.readLine()) != null)
  {
  sb.append(line); //append the lines to the string
  sb.append('\n'); //append new line
  } //end while
  </code></pre>
  
  <p>where <code>sb</code> is a StringBuffer</p>