IntelliJ Idea debug/run console; System.out.flush not flushing

醉酒当歌 提交于 2019-12-10 15:38:47

问题


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.

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, 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.


回答1:


Tested on version 2016.3.5, the problem is still there.

The bug is caused by all characters after "\r" will be cleared in the console.

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.

The workaround is to move "\r" to the beginning of the string. For example, "\rTest" will see the output in console.

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");



回答2:


Until this issue is not resolved, current workaround is to use -Deditable.java.test.console=true (specify it in idea.properties).



来源:https://stackoverflow.com/questions/39798849/intellij-idea-debug-run-console-system-out-flush-not-flushing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!