Nyx
Nyx

Reputation: 1374

New Line in File Writer From Existing Multi-Lined String

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

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533442

You can replace \n with the platform new line with.

bufferedWriter.write(test.replaceAll("\n", System,getProperty("line.separator"));

Upvotes: 1

Bohemian
Bohemian

Reputation: 424963

Use text.split("\n") to create multiple lines and write them separately.

Upvotes: 1

Related Questions