aspose file tools*
The moose likes Java in General and the fly likes Problem using a String containing large number of characters. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem using a String containing large number of characters." Watch "Problem using a String containing large number of characters." New topic
Author

Problem using a String containing large number of characters.

Rohan Chaudhari
Greenhorn

Joined: Mar 18, 2006
Posts: 10
Hi,

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.

Thanks and Regards,
Rohan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.

So, say you have

textArea.setText(myString);

change it to

System.out.println(myString);
textAray.setText(myString);
System.out.println(textArea.getText());

(I didn't try it, so I might have misremembered the exact method names. But you should get the idea...)
Rohan Chaudhari
Greenhorn

Joined: Mar 18, 2006
Posts: 10
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?
Eiji Seki
Ranch Hand

Joined: Feb 15, 2006
Posts: 88
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.


SCJD URLyBird (WIP)<br />SCJP 1.5
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
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
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
I agree with Eiji and Williams approach. In the short term to get over this, try to run your JVM with more heap space.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Rohan Chaudhari
Greenhorn

Joined: Mar 18, 2006
Posts: 10
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.

For writting :

FileWriter writer = new FileWriter("c:\\BOEExportData.txt");

BufferedWriter bufWriter = new BufferedWriter(writer);
bufWriter.wriwrite(ret.toString(),0,ret.length());
bufWriter.newLine();
bufWriter.close();
writer.close();

For reading:

FileReader reader = new java.io.FileReader("c:\\BOEExportData.txt");
BufferedReader bufReader = new BufferedReader(reader);
textPreview.read(bufReader,"My Value");

The above code has solved my problem.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

I'm with Eiji on this, you need a temporary file, dont try to display it all at one time.

Why would you reinvent the operating system's usually complicated paging algorithm by using a partially optimal algorithm of your own?


Tony Morris
Java Q&A (FAQ, Trivia)
Rohan Chaudhari
Greenhorn

Joined: Mar 18, 2006
Posts: 10
I am back with my famous large String manupulation problem. As per the previous suggestions, I am using the BufferedWriters and BufferedReaders to read and write the data resp.

First I am storing all the data in the temporary file using BufferedWriter and then displaying it in the JTextArea using the below code:


For writting :

FileWriter writer = new FileWriter(filePath + fileName);
java.io.BufferedWriter bufWriter = new java.io.BufferedWriter(writer);
bufWriter.write(tstBuffer.toString(),0,ret.length());
bufWriter.newLine();

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:

java.io.FileReader reader = new java.io.FileReader(filePath + fileName);
java.io.BufferedReader bufReader = new java.io.BufferedReader(reader);
textPreview.read(bufReader,"My Value");

For large amount of file, my program throws an OutOfMemory exception in the
above code.

Is there any way by which I can stop the JVM's memory to be exhausted?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

instead of

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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem using a String containing large number of characters.