Writing a “\\n” in a text file

爱⌒轻易说出口 提交于 2019-12-05 09:03:23
Charles Keepax

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.

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

you have to escape the backslash, so double it:

out("\\n")

Do you mean

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

instead of

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

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

What you want is to print two characters, namely \ and n. According to your language's manual, to print \ you must escape it. n is not among the characters you must escape. Therefore, you write \\ and n, thus \\n.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!