printf 中 Java 的“%n”是怎么回事?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1883345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:32:37  来源:igfitidea点击:

What's up with Java's "%n" in printf?

javanewlineprintf

提问by ripper234

I'm reading Effective Java and it uses %nfor the newline character everywhere. I have used \nrather successfully for newline in Java programs.

我正在阅读 Effective Java,它%n在任何地方都用于换行符。我\n在 Java 程序中相当成功地使用了换行符。

Which is the 'correct' one? What's wrong with \n? Why did Java change this C convention?

哪个是“正确”的?怎么了\n?为什么 Java 改变了这个 C 约定?

采纳答案by Bill K

From a quick google:

从快速谷歌:

There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A "\n" can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas"\n" is not.

还有一个说明符与参数不对应。输出换行符的是“%n”。在某些情况下也可以使用 "\n",但由于 "%n" 始终输出正确的特定于平台的行分隔符,因此它可以跨平台移植,而 "\n" 则不是。

Please refer https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

请参考 https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Original source

原始来源

回答by Gregory Pakosz

%nis portable across platforms \nis not.

%n跨平台\n可移植 不是。

See the formatting string syntax in the reference documentation:

请参阅参考文档中格式化字符串语法

'n' line separator The result is the platform-specific line separator

'n' 行分隔符 结果是平台特定的行分隔符

回答by Craig Putnam

While \nis the correct newline character for Unix-based systems, other systems may use different characters to represent the end of a line. In particular, Windows system use \r\n, and early MacOS systems used \r.

虽然\n是基于 Unix 的系统的正确换行符,但其他系统可能使用不同的字符来表示行的结尾。特别是Windows系统使用\r\n,以及早期的MacOS系统使用\r

By using %nin your format string, you tell Java to use the value returned by System.getProperty("line.separator"), which is the line separator for the current system.

通过%n在格式字符串中使用,您可以告诉 Java 使用由 返回的值System.getProperty("line.separator"),它是当前系统的行分隔符。

回答by George Forman

Warning: if you're doing NETWORKING code, you might prefer the certainty of \n, as opposed to %n which may send different characters across the network, depending what platform it's running on.

警告:如果你正在做网络代码,你可能更喜欢 \n 的确定性,而不是 %n 可能会通过网络发送不同的字符,这取决于它在什么平台上运行。

回答by Peter Green

"correct" depends on what exactly it is you are trying to do.

“正确”取决于您究竟要做什么。

\n will always give you a "unix style" line ending. \r\n will always give you a "dos style" line ending. %n will give you the line ending for the platform you are running on

\n 总是会给你一个“unix 风格”的行尾。\r\n 总是会给你一个“dos 风格”的行尾。%n 将为您提供正在运行的平台的行尾

C handles this differently. You can choose to open a file in either "text" or "binary" mode. If you open the file in binary mode \n will give you a "unix style" line ending and "\r\n" will give you a "dos style" line ending. If you open the file in "text" mode on a dos/windows system then when you write \n the file handling code converts it to \r\n. So by opening a file in text mode and using \n you get the platform specific line ending.

C 对此有不同的处理方式。您可以选择以“文本”或“二进制”模式打开文件。如果你以二进制模式打开文件 \n 会给你一个“unix 风格”的行尾,而“\r\n”会给你一个“dos 风格”的行尾。如果您在 dos/windows 系统上以“文本”模式打开文件,那么当您编写 \n 文件处理代码时,会将其转换为 \r\n。因此,通过以文本模式打开文件并使用 \n,您可以获得特定于平台的行结尾。

I can see why the designers of java didn't want to replicate C's hacky ideas regarding "text" and "binary" file modes.

我可以理解为什么 java 的设计者不想复制 C 的关于“文本”和“二进制”文件模式的hacky 想法。

回答by Fathah Rehman P

In java, \nalways generate \u000Alinefeed character. To get correct line separator for particular platform use %n.

在java中,\n总是生成\u000A换行符。要为特定平台使用正确的行分隔符%n

So use \nwhen you are sure that you need \u000Alinefeed character, for example in networking.
In all other situations use %n

因此,\n当您确定需要\u000A换行符时使用,例如在网络中。
在所有其他情况下使用%n

回答by user1677663

Notice these answers are only true when using System.out.printf()or System.out.format()or the Formatterobject. If you use %nin System.out.println(), it will simply produce a %n, not a newline.

请注意,这些答案仅在使用System.out.printf()orSystem.out.format()Formatterobject时才成立。如果您使用%nin System.out.println(),它只会生成一个%n,而不是换行符。

回答by Piyush Patel

%n format specifier is a line separator that's portable across operating systems. However, it cannot be used as an argument to System.out.print or System.out.println functions.

%n 格式说明符是一个可以跨操作系统移植的行分隔符。但是,它不能用作 System.out.print 或 System.out.println 函数的参数。

It is always recommended to use this new version of line separator above \n.

始终建议在 \n 上方使用此新版本的行分隔符。