Writing a “\n” in a text file

2019-04-29 12:04发布

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?

6条回答
Evening l夕情丶
2楼-- · 2019-04-29 12:09

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.

查看更多
SAY GOODBYE
3楼-- · 2019-04-29 12:10

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

查看更多
成全新的幸福
4楼-- · 2019-04-29 12:10

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

查看更多
该账号已被封号
5楼-- · 2019-04-29 12:16

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.

查看更多
看我几分像从前
6楼-- · 2019-04-29 12:23

Do you mean

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

instead of

pw.print("\n"); // print new line.
查看更多
你好瞎i
7楼-- · 2019-04-29 12:31

you have to escape the backslash, so double it:

out("\\n")
查看更多
登录 后发表回答