Java: BufferedWriter skips a new line

I use the following function to write a string to a file. The string is formatted with newline characters.

For instance, text = "sometext\nsomemoretext\nlastword";

I can see the newline characters of the output file when I do this:

type outputfile.txt

However, when I open the text in notepad, I do not see new lines. Everything is displayed on one line.

Why is this happening. How can I make sure that I am writing the text correctly so that I can correctly see (format) in notepad.

    private static void FlushText(String text, File file)
    {
        Writer writer = null;
        try
        {          
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(text);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        } 
        finally
        {
            try
            {
                if (writer != null)
                {
                    writer.close();
                }
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
+5
source share
2 answers

In windows, new lines are represented, conditionally, as a carriage return, followed by a line (CR + LF), i.e. \r\n.

On the New wikipedia page :

; , , ASCII CR/LF . Windows ( Wordpad ).

, :

text = "sometext\r\nsomemoretext\r\nlastword";

, System.getProperty("line.separator"); BufferedWriter, , .

+10

BufferedWriter.newLine() . , , .

+7

All Articles