问题
I am working with very large strings whose length range from 0 to 2*10^5.
When I try to print the strings on the console or using command line via System.out.println
, nothing shows up. Only strings/substrings with 4096 characters show up. Also, I get no errors.
I also tried to print the characters one at a time using System.out.print(chararray[i])
but to no avail.
I even tried tried to use the below but it did not work.
StringWriter stringWriter = new StringWriter();
BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);
bufferedWriter.write(str); //does not display
StringBuilder sb = new StringBuilder(str);
System.out.println(sb); //does not display
Displaying is important here since I am submitting my program on HackerRank and my program does not pass the test case due to this issue.
Can anyone please help?
Note: I know the strings are being stored properly since I was able to verify their lengths using str.length();
回答1:
By default, Eclipse limits the amount of text in the console output window. So if you are running a program which produces a lot of output, the Console will only display the last n lines and you lose the rest.
Here is a tip which will allow you to have unlimited output in the console:
- Go to Window > Preferences > Run/Debug > Console
- Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)
Credits
回答2:
Try dividing the string into multiple chunks and then write to a text file. You can refer below link to divide the string : How to split a string into equal parts and store it in a string array
来源:https://stackoverflow.com/questions/34535561/not-able-to-print-very-large-strings-in-java-neither-in-eclipse-nor-in-cmd