Search code examples
javatext-filesbufferedreader

Replace \n in BufferedWriter.write()?


I have a String that contains for example "Name: Foo \n Date: 12/13/11 \n"

When I do BufferedWriter.write(String) to a text file it actually outputs the \n is there anyway to do a replace on \n to something that when written to a text file will signify a line break?

Here is sample code.

String input = "Name: adfasd \n AN: asdfasdf \n";
Writer textOutput = null;
File file = new File("write.txt");  
textOutput = new BufferedWriter(new FileWriter(file));
textOutput.write( input );
textOutput.close();

The output would be

http://img7.imageshack.us/img7/7694/exampleve.png


Solution

  • Windows uses CR+LF line endings as opposed to LF endings. You should be able to use \r\n to get your desired result.

    Wikipedia has an article about newlines if you want to know more.