简体   繁体   中英

Debug with System.out.print in a loop (Eclipse)

To my surprise in the following program the Eclipse console doesn't print while in the loop. It prints only: "start: finish". When I use println instead it does. Of when I remove the comment it does too. Surprise: when I copy the lines "start: finish" in the console, the clipboard does contain all printed numbers. Interesting to know waht is the cause of this behaviour?

public static void main(String[] args) {
        System.out.print("start: ");
        for (int i = 0; i < 10000; i++) {
            // if (i > 1000 && i < 1010)
                System.out.print(i + " ");
        }
        System.out.println("finish");
    }

Java中的Println或print(“ \\ n”)在C&C ++中,刷新缓冲区并强制其打印缓冲区中的所有内容,但是当使用print白色而不用任何\\ n(换行)时,不一定是打印缓冲区

Well, you have a very, very long line there. So, it is quite possible that you have exceeded the maximum line length that the Eclipse console can reliably display. The content probably has been printed there (as evidenced by the fact that it appeared in your clipboard when you copy and paste) but doesn't render well.

Behavior confirmed. Strange, must have something to do with the eclipse wrapped output stream. Eclipse 3.5 by me. Perhaps the internal buffering throws away output if it is not properly flushed() or too wide for a line.

package tests;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class EclipsePrint {
    public static void main(String[] args) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintStream out = System.out;
        System.setOut(new PrintStream(new BufferedOutputStream(bout), true));
        System.out.print("start: ");
        for (int i = 0; i < 10000; i++) {
                // if (i > 1000 && i < 1010)
                        System.out.print(i + " ");
        }
        System.out.println("finish");
        out.println(bout.size());
        out.println(bout.toString());

        bout = new ByteArrayOutputStream();
        System.setOut(new PrintStream(new BufferedOutputStream(bout), true));
        System.out.print("start: ");
        for (int i = 0; i < 1000; i++) {
                // if (i > 1000 && i < 1010)
                        System.out.print(i + " ");
        }
        System.out.println("finish");
        out.println(bout.size());
        out.println(bout.toString());
    }
}

There is a console settings at Run/Debug:Console:console buffer size (characters) in eclipse which allows you to use up to 10^6 chars as console buffer. Your first loop exceeds it.

Tried that on my eclipse 3.4/Mac OSx It printed well when the upper limit was 1000, but for 10000, the display truncated.

Instead of System.out.print, use System.out.println; that's for each line and it works fine for me for 10000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM