Writing a "\n" in a text file

16,340

Solution 1

The \ character escapes the next character, as you say \n will create a newline. If you wish to output an actual \, you need to write:

"\\n"

That way the first slash escapes the second slash, generating an actual slash rather than escaping the n.

Solution 2

Use "\\n". The first backslash escapes the second one and as a result one is printed to your output.

Solution 3

you have to escape the backslash, so double it:

out("\\n")

Solution 4

Do you mean

pw.print("\\n"); // print \ and n

instead of

pw.print("\n"); // print new line.

Solution 5

You need to escape the \. This can be done by entering \\.

Share:
16,340
Admin
Author by

Admin

Updated on June 20, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to write a string \n in a text file. But the result in the text file is a newline. I know that the \n represents a newline. But in my case, I really need to see the String \n, not the newline. How can I solve this?