Java中的\r回车和\f换页是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19025868/
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 13:29:56  来源:igfitidea点击:

What is the meaning \r carriage return and \f form feed in Java?

java

提问by bhv

What is the meaning of \rcarriage return and /fform feed in Java? Can anyone give a small example of when we have to use these types of symbols?

Java中\r回车和换页是什么意思/f?谁能举一个小例子来说明我们何时必须使用这些类型的符号?

System.out.println("hello world \r rafter \n nafter \f fafter");

I don't know what is what in that above.

我不知道上面的内容是什么。

The output I got is:

我得到的输出是:

hello world 
 r-after 
 n-after  f-after

I got \ras new line and \nfor new line and with \fa box appears for me.

我得到了\r新线和\n新线,并\f为我出现了一个框。

采纳答案by T.J. Crowder

You rarely use either of them (in isolation) in modern applications.

您很少在现代应用程序中使用它们中的任何一个(单独使用)。

\r(carriage return) is what it sounds like ifyou're familiar with old-fashioned manual typewriters: It moves the "carriage" (the roll the paper is fed through) back to the beginning of the line. On a terminal (or similar), it moves the output point back to the beginning of the line, without moving down a line (usually).

\r(回车)如果您熟悉老式手动打字机,听起来就是这样:它将“托架”(纸张通过的卷筒)移回行的开头。在终端(或类似终端)上,它将输出点移回行的开头,而不向下移动一行(通常)。

\fis (as you said) a formfeed, it tells old-fashioned printers to start a new page. In computer documents, it's sometimes used to indicate a page break.

\f是(如你所说)一个formfeed,它告诉老式打印机开始一个新页面。在计算机文档中,它有时用于表示分页符。

Rounding things out, there's also line feed (aka "newline"): \n. This means "move down a line." In some terminals, it just moves down a line without doing a carriage return; on others, it does both.

四舍五入的东西出来,这里还有换行(又名“换行”) \n。这意味着“沿着一条线移动”。在某些终端中,它只是向下移动一行而不做回车;在其他方面,它两者兼而有之。

\n(LF, newline) is the standard text file line break on *nix and derived operating systems. \r\n(CRLF) is the standard text file line break on DOS and Windows. Back in the day, \r(on its own) was the standard line break in text files on the Mac, prior to Mac OS X (which is *nix-derived).

\n(LF, newline) 是 *nix 和派生操作系统上的标准文本文件换行符。\r\n(CRLF) 是 DOS 和 Windows 上的标准文本文件换行符。在过去,\r(就其本身而言)是 Mac OS X(*nix 派生)之前 Mac 上文本文件中的标准换行符。

回答by Marko Topolnik

To see the difference you must run your code from a command prompt. Eclipse's Console (and similar for other IDEs) does not simulate the behavior of a full TTY terminal and will move to the next line for both \rand \n. On the command line, however, \rwill only move the cursor back to the beginning of the currentline. This is very convenient when you want to show live progress of your program without scrolling.

要查看差异,您必须从命令提示符运行代码。Eclipse的控制台(和其他IDE类似)不模拟一个完整的TTY终端的行为,并会移动到两个下一行\r\n。然而,在命令行上,\r只会将光标移回当前行的开头。当您想在不滚动的情况下显示程序的实时进度时,这非常方便。

The form-feed signal is harder to predict and not as useful. It may have the effect of clearing the terminal window in some cases.

换页信号更难预测,也没有那么有用。在某些情况下,它可能具有清除终端窗口的效果。

回答by Ingo

Carriage return and form feed are terminal or printer control characters and have no meaning whatsoever in Java.

回车和换页是终端或打印机控制字符,在 Java 中没有任何意义。

In a terminal emulation, when you print a \r, the next character will possibly get printed at the first row of the currentline (overwriting what is there already).

在终端仿真中,当您打印 \r 时,下一个字符可能会打印在当前行的第一行(覆盖已有的)。

A form feed, when printed to a terminal may or may not clear the screen. On a printer, it should cause the current page to get ejected, so that printing continues on a new page.

换页在打印到终端时可能会也可能不会清除屏幕。在打印机上,它应该导致当前页面被弹出,以便在新页面上继续打印。

But, as said before, neither the terminal (emulation) nor the printer do care whther the control character was printed by a Java program.

但是,如前所述,终端(仿真)和打印机都不关心控制字符是否由 Java 程序打印。

回答by AAYU

\fis similar to \n, the difference is that the new line gets indented.

\f类似于\n,不同之处在于新行缩进。

eg: "hello it was \f great meeting you"

例如:“你好,很高兴见到你”

it gives output:

它给出了输出:

 hello it was 
              great meeting you