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?
Are you experiencing this within Eclipse? If yes:
EDIT:
Go to Window > Preferences > Run/Debug > Console
Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)
Source
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
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.
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?
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.
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.
Related
This is not the basic Scanner(System.in).
I have some recursive methods that I saw online that does exactly what I want but now I need to count the number of lines that have been "outputed" to the console.
So, I run the program. It does some computations and then it writes on the console some lines (as you can see above) and I need to , in the end on the computations (after the execution of the main), count the number of that lines and write it to somewhere.
Example of the output:
Does anyone know how it can be done?
Thanks.
You could use the class TeeOutputStream from Apache Common IO that split the outputs into two streams. This way you can still publish your stream in the default output and log them in a file. With the outputs in the file you can do whatever you wanted (count lines...).
Why don´t you declare a variable counter and initialize it with 0. After every System.out.println() statement, you increment the counter by adding counter++.
As final Statement in your main-Method, you print the variable to the console:
System.out.println("Counter: "+counter");
If that doesn´t work for you, you could still write the output of the program to a file instead of the console, read that file afterwards and look for line breaks.
in our company we recently changed the used line width from like 80 to 150.
Now it is quite annoying that all code is - of course - wrapped after 100 characters, as this was the previous setting.
We use save actions to run the format settings and it all works wonderful in the one direction: wrap too large lines.
But what I need now: UNWRAP me all lines as I now got enough space available, USE it. Is there a possibility to do that in form of a format setting? I couldn't find any
I now got this line (extremly) wrapped as there were not enough space:
final List<SomeSuperDuperType>
mySuperDuperListOfSuperDuperTypes =
CrazyUtils.gimmeSomeCrazyStuffAsList(
parameter1, parameter2);
Now, with more space available, this code is still correctly formatted as it doesn't exceed the limit. Though I want to actually USE that extra space and make the line now like this (length=133):
final List<SomeSuperDuperType> mySuperDuperListOfSuperDuperTypes = CrazyUtils.gimmeSomeCrazyStuffAsList(parameter1, parameter2);
On Eclipse, go to Window>Preferences>Java>Code Style>Formatter, edit the active profile, and you'll find what you're looking for under the tab Line Wrapping.
Then, select all your code (CTRL+A) and use CTRL+Shift+F to format the text using these new setttings.
I have a testing program (that I didn't write) that is giving a very weird error, which only happens some of the time. At the end of the program a string that contains the test report is output using
logger.warn(reportString);
Anywhere from 1/10 to 1/3 tries (it varies) this string isn't being displayed, but everything before it is.
Before this I added this line to ensure that the string is always created properly, which it is since this gives the same length every time.
logger.info("Length: " + reportString.length());
To experiment I added another logging line after, so the end of the program now looks like this:
logger.info("Length: " + reportString.length());
logger.warn(reportString);
logger.info("REACHED END OF PROGRAM");
When doing this the reportString always seems to be printed/logged, but the "REACHED END OF PROGRAM" string is only printed/logged about 50% of the time. Maybe it's something with my windows command prompt? I know this is a pretty vague problem but if anyone has any ideas they would be greatly appreciated.
Maybe you need to flush the all buffered logs to send all to the output. Add the next:
logger.warn(reportString);
LogManager.shutdown();
Just in case you have it in your code, you can try removing the sentence:
System.exit(0);
This probably has to do with how log4j buffers the data it outputs, try explicitly adding \n in the last string the logger prints, and see if it prints consistently now.
I have two threads - one awaits for input and the other is printing the debugging info.
However only one console window, so I can't type 'exit' (or whatever to stop the process), because System.out.println constantly prints the stuff. Can I have two separate console windows for each?
P.S. I wouldn't want to use Swing just for this purpose - there must be a way.
The only way I could think of would be to have two difference processes and a link betweeen the two processes. But I don't have a clue as to how to do that. Perhaps your best bet is to use the JOptionPane class.
While you stated that you don't want to use Swing, I believe that JOptionPane would be the best option for you. Simply using JOptionPane.showInputDialog is a fast way to solve your issue. Here's a link to JOptionPane's JavaDoc.
If you really can't use Swing, there's always the option to press Ctrl + C to stop the process.
A final option would be to buffer the output and only write it after the input. After you receive input, you would flush the buffer and then deal with the input. In this manner, you would prevent the application from closing before the buffer is flushed. There are two ways to do this:
You can use a BufferedWriter with a very large size (maybe 100,000?) and store this as a static variable. Instead of calling System.out.println(), you could call MyClass.out.println()
You could override System using System.setOut(). You would create your own PrintWriter that would take any input and send it to a LinkedList (or your own LinkedList designed for chars, if you choose). I suggest you use a linked list because appending is O(1) for a linked list while appending is O(n) for an array list.
Edit:
As for hmjd's suggestion (file writing), you would do that like this:
System.setOut(new FileWriter(new File(myFileName)));
Log to a file then go into another window and tail the file (in unix/mac use "tail -f filename", in another os--install unix/cygwin!)
This keeps your log separate from your console and makes it persistent as well.
There are a lot of logging utilities out there that will help with this and will even help a bit more by telling you what file a given line is coming from.
Your question is similar to this one, so I think the answer is the same. However, maybe this question might be right for you.
This question already has answers here:
How to clear the console?
(14 answers)
Closed 7 years ago.
Is there any option to clear the screen in java as clrscr() in C.
As dirty hacks go, I like msparer's solution. An even dirtier method that I've seen used (I would never do this myself. I swear. Really.) is to write a bunch of newlines to the console. This doesn't clear the screen at all, but creates the illusion of a clear screen to the user.
char c = '\n';
int length = 25;
char[] chars = new char[length];
Arrays.fill(chars, c);
System.out.print(String.valueOf(chars));
If you're talking about a console application, then there isn't a clear screen option AFAIK. A quite dirty option would be to invoke the clear screen command of the underlying OS.
Then it's something like
Runtime.getRuntime().exec("cls");
for Windows or
Runtime.getRuntime().exec("clear");
for a load of other OS. You can find out the OS with System.getProperty("os.name").
If you're talking about the console, then no. Writing to the console is just a special case of an output stream. Output streams don't know anything about the screen, as they can be just as easily redirected to a file or another system device.
For any console which supports ANSI escapes the following would work (would e.g. work in Win98 console).
private final String ANSI_CLS = "\u001b[2J";
....
System.out.print(ANSI_CLS);
System.out.flush();
...
Starting with Win NT this won't work anymore and you can either
Do a JNI call (e.g. like here: Java: Clear console and control attributes
Or write out a bunch of empty lines
Otherwise you are out of luck.
And btw. you must keep in mind that System.out and System.err don't have to be console they could be set to what ever (writing into a file e.g.) an usecase where clearing the screen wouldn't make any sense at all.
On linux, you can do something like:
System.out.println("\f");
You can also use Jcurses
To clear the screen just type:
System.out.print('\u000C');
You can also try ANSI Escape Codes:
If your terminal support them, try something like this:
System.out.print("\033[2J\033[1;1H");
You can include \0333[1;1H to be sure if \0333[2J does not move the cursor in the upper left corner.
More specifically:
033 is the octal of ESC
2J is for clearing the entire console/terminal screen
1;1H moves the cursor to row 1 and column 1
Jansi is an excellent workaround. I am an amateur coder and Jansi is easy to setup especially with Eclipse.
The following is a link to the homepage of Jansi:
http://jansi.fusesource.org/
The following is a link to a site containing a code as a demonstration of AnsiConsole class contained in the Jansi package:
http://www.rgagnon.com/javadetails/java-0047.html
For Windows, Java Console API project provides functionality to determine console size and set cursor position. Clearing the screen is trivial with that. It's a version 0.2 now so it's not exactly production ready, but it works.
Alternatively, you can simply print out some new lines via System.out.println(). 640 should be enough for everybody :-) It's not the same as clearing screen, but for user's intents and purposes it'd do.
you should give a try with JNA and try mapping native libraries:
on linux you must map C functions from ncurses library
on windows you must map functions from both msvcrt and kernel32, as clearly stated here
PS
let me known if you need some sample code