How can I return to the start of a line and overwrite what has already been output on the console? The following does not appear to work:
如何返回到行的开头并覆盖控制台上已输出的内容?以下似乎不起作用:
System.out.print(mystuff+'\r');
3 个解决方案
#1
37
I suspect that your cursor IS moving to the front of the line. The text you already have isn't disappearing because you haven't overwritten it with anything. You could output spaces to blank the line and then add another \r.
我怀疑你的光标正在移动到行的前面。你已经拥有的文字并没有消失,因为你没有用任何东西覆盖它。您可以输出空格以清空该行,然后添加另一个\ r。
I just tested the following on Windows XP and AIX and it works as expected:
我刚刚在Windows XP和AIX上测试了以下内容,它按预期工作:
public class Foo {
public static void main(String[] args) throws Exception {
System.out.print("old line");
Thread.sleep(3000);
System.out.print("\rnew");
}
}
I get "old line" printed, a 3 second delay, and then "old line" changes to "new line"
我打印“旧线”,延迟3秒,然后“旧线”变为“新线”
I intentionally made the first line longer than the second to demonstrate that if you want to erase the whole line you'd have to overwrite the end with spaces.
我故意使第一行比第二行更长,以证明如果你想要删除整行,你必须用空格覆盖末尾。
Also note that the "\b" escape sequence will back up 1 space, instead of to the beginning of the line. So if you only wanted to erase the last 2 characters, you could write:
另请注意,“\ b”转义序列将备份1个空格,而不是行的开头。所以如果你只想删除最后2个字符,你可以写:
System.out.println("foo\b\bun")
and get "fun".
并获得“乐趣”。
#2
7
My guess (And it is a guess) would be that '\r' does work, but the console you're using doesn't treat it as you'd expect. Which console are you using? If it's something like console output in your IDE, have you tried it on a real command-line instead?
我的猜测(这是一个猜测)将是'\ r'确实有效,但你正在使用的控制台并不像你期望的那样对待它。你在使用哪个控制台?如果它类似于IDE中的控制台输出,那么您是否在真正的命令行上尝试过它?
#3
3
If you just want to write a new line to the console, you should use the println() method:
如果您只想在控制台中写一个新行,则应使用println()方法:
System.out.println(mystuff);
However, this will not delete what is already on the line. Actually, since System.out is a PrintStream, which is a type of OutputStream, that is basically hard to do, although you may find terminal-specific ways to do it.
但是,这不会删除已在线上的内容。实际上,由于System.out是一个PrintStream,它是一种OutputStream,基本上很难做到,尽管你可能会发现特定于终端的方法。
You might have better luck using a Java implementation of a curses library, such as JavaCurses.
使用curses库的Java实现(例如JavaCurses)可能会更好。
#1
37
I suspect that your cursor IS moving to the front of the line. The text you already have isn't disappearing because you haven't overwritten it with anything. You could output spaces to blank the line and then add another \r.
我怀疑你的光标正在移动到行的前面。你已经拥有的文字并没有消失,因为你没有用任何东西覆盖它。您可以输出空格以清空该行,然后添加另一个\ r。
I just tested the following on Windows XP and AIX and it works as expected:
我刚刚在Windows XP和AIX上测试了以下内容,它按预期工作:
public class Foo {
public static void main(String[] args) throws Exception {
System.out.print("old line");
Thread.sleep(3000);
System.out.print("\rnew");
}
}
I get "old line" printed, a 3 second delay, and then "old line" changes to "new line"
我打印“旧线”,延迟3秒,然后“旧线”变为“新线”
I intentionally made the first line longer than the second to demonstrate that if you want to erase the whole line you'd have to overwrite the end with spaces.
我故意使第一行比第二行更长,以证明如果你想要删除整行,你必须用空格覆盖末尾。
Also note that the "\b" escape sequence will back up 1 space, instead of to the beginning of the line. So if you only wanted to erase the last 2 characters, you could write:
另请注意,“\ b”转义序列将备份1个空格,而不是行的开头。所以如果你只想删除最后2个字符,你可以写:
System.out.println("foo\b\bun")
and get "fun".
并获得“乐趣”。
#2
7
My guess (And it is a guess) would be that '\r' does work, but the console you're using doesn't treat it as you'd expect. Which console are you using? If it's something like console output in your IDE, have you tried it on a real command-line instead?
我的猜测(这是一个猜测)将是'\ r'确实有效,但你正在使用的控制台并不像你期望的那样对待它。你在使用哪个控制台?如果它类似于IDE中的控制台输出,那么您是否在真正的命令行上尝试过它?
#3
3
If you just want to write a new line to the console, you should use the println() method:
如果您只想在控制台中写一个新行,则应使用println()方法:
System.out.println(mystuff);
However, this will not delete what is already on the line. Actually, since System.out is a PrintStream, which is a type of OutputStream, that is basically hard to do, although you may find terminal-specific ways to do it.
但是,这不会删除已在线上的内容。实际上,由于System.out是一个PrintStream,它是一种OutputStream,基本上很难做到,尽管你可能会发现特定于终端的方法。
You might have better luck using a Java implementation of a curses library, such as JavaCurses.
使用curses库的Java实现(例如JavaCurses)可能会更好。