问题描述
在过去的几天里,这一直困扰着我,因为它曾经可以工作。我升级了intellij,但现在不起作用。我不想回去,但我需要一个答案。
This has been bugging me for the last couple days, because it used to work. I upgraded my intellij and now it doesn't work. I don't want to go back, but I need an answer.
因此,我正在编写一个控制台应用程序,并且在其运行时,我想要一个外壳显示进度。它在运行时可以正常工作,但是当我在IntelliJ Idea中调试时,除非缓冲区包含换行符,否则System.out.flush()不会刷新到控制台。我编写了以下单元测试,以了解它是我还是我的应用程序。
So, I'm writing a console application, and while it runs, I want to have a shell'd out display of progress. It works fine when it's running, but when I'm debugging in IntelliJ Idea, the System.out.flush() won't flush to the console unless the buffer contains a newline. I wrote the following unit test to find out if it was me or my application.
@Test
public void flushTest() {
int loops = 100;
for (int i = 0 ; i < 100 ; ++i) {
System.out.print("This is a print, does it flush : " + i + "\r");
if (i %10 == 0) {
System.out.print("\n");
}
System.out.flush();
}
}
Spoiler,它是应用程序。我不知道更改了什么,但是如果不进行完整构建并在命令行上运行,则很难调试显示问题。如果有人可以帮助我弄清楚为什么更改和/或帮助我解决它,以便它可以正常刷新,我将非常感谢。
Spoiler, it is the application. I don't know what's changed, but it makes it very difficult to debug display issues without doing a full build and running on the command line. If someone can help me figure out why this changed and/or help me fix it so It'll flush properly, I'd be very grateful.
推荐答案
在版本2016.3.5上测试,问题仍然存在。
Tested on version 2016.3.5, the problem is still there.
该错误由之后之后的所有字符引起 r将在控制台中清除。
The bug is caused by all characters after "\r" will be cleared in the console.
例如,使用光标 \r向左移动光标后,将使用 Test\r字符串将 Test写入控制台,则清除所有字符 Test。因此,您什么也看不到。
For example, using a string "Test\r" will be got "Test" written to console, after cursor is moved to the left by the "\r", all the characters "Test" is cleared. So you won't see anything.
解决方法是将 \r移到字符串的开头。例如, \rTest将在控制台中看到输出。
The workaround is to move "\r" to the beginning of the string. For example, "\rTest" will see the output in console.
以下代码将说明该错误。您将看到 0000和 111出现1秒钟,然后全部清除为 22。
The following codes will illustrate the bug. You will see "0000" and "111" will appear for 1 second and then all are cleared by "22".
System.out.print("0000\r");
Thread.sleep(1000);
System.out.print("111\r");
Thread.sleep(1000);
System.out.print("22\r");
这篇关于IntelliJ Idea调试/运行控制台; System.out.Flush不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!