Reputation: 1374
I am trying to output a multi-line string to a file using FileWriter. How can I make it recognize the lines and automatically use "output.newLine"?
Example String
static String test = "This \n" + "is \n" + "a \n" + "sample \n" + "string."
public static void testMethod (String test){
FileWriter fileWriter = new FileWriter ("output.txt", true)
BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);
bufferedWriter.write (test);
bufferedWriter.close ();
}
Upvotes: 1
Views: 3579
Reputation: 533442
You can replace \n
with the platform new line with.
bufferedWriter.write(test.replaceAll("\n", System,getProperty("line.separator"));
Upvotes: 1
Reputation: 424963
Use text.split("\n")
to create multiple lines and write them separately.
Upvotes: 1