-->

Character limit for System.out.println() in Java

2019-01-15 18:41发布

问题:

Is there any character limit for the output of Java's System.out.println(String x) statement?

When I try to print some XML from a web service call using System.out.println(), only a portion of it is actually printed in the console.

The XML string that I am trying to print is huge.

Why is this happening?

回答1:

Are you experiencing this within Eclipse? If yes:

EDIT:

  1. Go to Window > Preferences > Run/Debug > Console
  2. Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)

Source



回答2:

My guess is that you only see the last part of the String because the console has a limited number of lines it can display.

Consider logging to a file from Java, or redirecting the standard output from the program to a file:

java com.foo.bar.Main > output.log


回答3:

If you are using Eclipse, it is because there is a limit on the capacity of the Eclipse output console. See this SO question: How do I increase the capacity of the Eclipse output console?



回答4:

You're limited by the maximum size of a Java String. That's all. This should be the equivalent of length Integer.MAX_VALUE (2147483647), which is the max size of an array, since a String is a char array.

Otherwise, it's the Eclipse console capacity limit, as others have said.



回答5:

I know that printing very long strings into the Eclipse console results in part or all of the string becoming invisible. You may want to break your xml into chunks. If you are only seeing the tail part of the xml then I'd guess its your console buffer trimming off part of it. @Quaylar posted a link about this.



回答6:

There isn't really an explicit maximum, but the offset in the string is determined by int, so Integer.MaxValue would be one limitation IMO. It also would depend on your available memory.

Your best bet would be to stream the output and write portions at a time to ensure you get it all.