I hv written a program which exports the data in a text file. When my program is run, the data is displayed to the user in a JTextArea.
I am using StringBuffer class to construct the data and convert it into the String object while displaying it in the Text Area. Now, the problem is the generated String lengh is very large. Due to which, the text area is blank.
I wanted to ask if is there any limit to the maximum number of characters which can be stored in a String object or any limit to the number of characters which can be displayed in the JTextArea.
A Java String may contain up to Integer.MAX_VALUE characters.
Did you take a look at the generated HTML code itself? Does it contain the text?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I'm confused. How did HTML enter into the equation? Isn't this a Swing issue (question was about JTextArea)?
I don't know why the widget ends up blank, maybe something to do with needing to use it in a JScrollPane and specifying rows/columns/wrapping when the String is big, or maybe to use a Document to have a more powerful model for working with smaller pieces of the text.
Reid - SCJP2 (April 2002)
Joined: Jul 11, 2001
Originally posted by Reid M. Pinchback: I'm confused. How did HTML enter into the equation? Isn't this a Swing issue (question was about JTextArea)?
Ah, somehow I missed the "J" and thought this was about HTML. Sorry - only excuse is the cold I'm currently suffering... :roll:
Rohan, one way to find out whether this is a problem of the String actually being empty, or of the text area not showing the string correctly, would be to additionally print the string to console using System.out.
(I didn't try it, so I might have misremembered the exact method names. But you should get the idea...)
Joined: Mar 18, 2006
Thanks for the response. I hv tried one more thing. I created a string of around 50,00000 characters and tried to print it. Due to the string being so large it threw an OutOfMemory excpetion.
As mentioned by Iija, a String can contain upto Interger.MAX_VALUE characters.But unfortunately for me, I don't know the size of the String before hand. My application is used by the customer to generated very large amount of data. But due its large size, it's occupying the whole of JVM's heap.
Is there any way I can manage the memory efficiently? Is there any other way by which I can print the such large amount of data?
Try using a temporary file for storing the data. Read from it to show the user. Even if the JTextArea supports it, it is not a good idea to show all the data, though... try breaking it into pages. If the user needs all the text, really, just give him on a text file instead.
I'm with Eiji on this, you need a temporary file, dont try to display it all at one time. As you write to the temporary file, keep track of the file positions of the start of logical "pages" in some data structure. Maybe an index of first lines of chapters, table heads, whatever. Then when the temporary file is closed, you can use RandomAccessFile to jump to the start of these "pages". JTextArea, etc behave much better with modest size text chunks. Bill
Thanks for your suggestion Eiji. Now I am using BufferedWriter for writting the exported data to a temporary file and then using the BufferedReader to display it the JTextArea. Below is the code snippet I implemented.
However, the problem which I face in the above code is : The Buffered Writer stores the data in temp buffers. It won't write the data to the file unless and untill u call the close() or the flush() method. This will cause the JVM's heap space to be consumed and hence causing the program to throw and OutOfMemory exception at some point of the time.
To resolve this issue, I am flushing the buffers from time to time to free up the JVM's heap.
Now for displaying the file data in JTextArea, I am using the belowcode:
Have you tried raising the amount of heap space available? The default avilable is 64 Mb, so try
java -Xmx128M MyClass
to set it to 128 Mb. Or try other numbers, see how it works. Documentation of -Xmx is here.
You may also be able to reduce some of the memory use in your code. You're building a really big StringBuffer, then converting it to a String, then writing it to a Writer. If you're using JDK 5, the toString() call is probably doubling your memory usage right there. And you can probably reduce it a lot more than that. Couldn't you just skip the StringBuffer and String entirely, and whatever stuff you're adding to the StringBuffer in the first place, just send it to the Writer instead? Unless you need the StringBuffer for something else, this seems somewhat simpler to me. If you can't remove the StringBuffer entirely, try using
as the former avoids the need to copy data from the StringBuffer to a new String before writing.
Also, I know that OutOfMemoryError often does not have a good stack trace attached, but it might be helpful to know just which part of the program the error is happening in. You may want to add various logging statements throughout your code so that you can tell from the output which parts have executed before the error. [ April 04, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Author and all-around good cowpoke
Joined: Mar 22, 2000
You appear to still be on the path of trying to get a JTextArea to display the whole file. This is just not going to be feasible for large files. For one thing, I suspect that in order to accommodate editing of the text, JTextArea will make a whole new copy of the characters, thus doubling your memory requirement. Bill