Search code examples
javastringfilewriter

Not writing String to file with formating


My string looks like this before printing: "goo\nfoo\nblah\n". It prints that fine with System.out.println but when I do this(where expand is my String):

     try {
         PrintWriter writer = new PrintWriter("blah.txt", "UTF-8");
         writer.println(expand);
         writer.close();
     } catch (IOException e) {
            e.printStackTrace();
    }
}

It doesn't print the newlines. How do I get it to write to the file with the newline characters?


Solution

  • If your using Windows, replace your \n with \r\n.