Writing a large string buffer to file cuts off portion

  • Thread starter Arun Kumar Srinivasan
  • Start date
A

Arun Kumar Srinivasan

Hi,
I am writing a program that generates a reasonably large result in
HTML format. The result is stored in the variable htmlContent which is
a StringBuffer object. Using "text/html" content type, the contents of
this variable is depicted on a JTextPane. There is a "Write to File"
button that allows the user to write the HTML data to a file. The code
for this is
// Code snippet Starts here
setOutput(); // Gets the File fName where the user
wants to write the file
if (fName!=null)
try {
FileWriter oWrite=new FileWriter(fName);
oWrite.write(htmlContent.toString());
}
catch (Exception e)
{
e.printStackTrace();
FrmMain.displayError(e.toString());
}
// Code snippet ends here
What happens is that after about 17000 characters, the file ends
abruptly. The result is that one table and a portion of another are
missing from the file. Can anyone please tell me what might be the
problem? I dont think there is any memroy problem because I had about
113 MB free during execution. Could there be some other restriction I
am not aware of? I once was able to write a HTML file with 77000+
characters through similar method (not sure if I used FileWriter
though). Please help.
Thanks in advance,
Arun Kumar Srinivasan
 
R

Ryan Stewart

Arun Kumar Srinivasan said:
Hi,
I am writing a program that generates a reasonably large result in
HTML format. The result is stored in the variable htmlContent which is
a StringBuffer object. Using "text/html" content type, the contents of
this variable is depicted on a JTextPane. There is a "Write to File"
button that allows the user to write the HTML data to a file. The code
for this is
// Code snippet Starts here
setOutput(); // Gets the File fName where the user
wants to write the file
if (fName!=null)
try {
FileWriter oWrite=new FileWriter(fName);
oWrite.write(htmlContent.toString());
}
catch (Exception e)
{
e.printStackTrace();
FrmMain.displayError(e.toString());
}
// Code snippet ends here
What happens is that after about 17000 characters, the file ends
abruptly. The result is that one table and a portion of another are
missing from the file. Can anyone please tell me what might be the
problem? I dont think there is any memroy problem because I had about
113 MB free during execution. Could there be some other restriction I
am not aware of? I once was able to write a HTML file with 77000+
characters through similar method (not sure if I used FileWriter
though). Please help.
Thanks in advance,
Arun Kumar Srinivasan

Unless I'm missing something, there's no way that your output line:
oWrite.write(htmlContent.toString());

will even compile, much less execute properly. FileWriter.write(String s) is
not a method. How about some real code, preferably executable? See
http://www.physci.org/codes/sscce.jsp
 
G

Gregory A. Swarthout

Hi,
I am writing a program that generates a reasonably large result in
HTML format. The result is stored in the variable htmlContent which is
a StringBuffer object. Using "text/html" content type, the contents of
this variable is depicted on a JTextPane. There is a "Write to File"
button that allows the user to write the HTML data to a file. The code
for this is
// Code snippet Starts here
setOutput(); // Gets the File fName where the user
wants to write the file
if (fName!=null)
try {
FileWriter oWrite=new FileWriter(fName);
oWrite.write(htmlContent.toString());
}
catch (Exception e)
{
e.printStackTrace();
FrmMain.displayError(e.toString());
}
// Code snippet ends here
What happens is that after about 17000 characters, the file ends
abruptly. The result is that one table and a portion of another are
missing from the file. Can anyone please tell me what might be the
problem? I dont think there is any memroy problem because I had about
113 MB free during execution. Could there be some other restriction I
am not aware of? I once was able to write a HTML file with 77000+
characters through similar method (not sure if I used FileWriter
though). Please help.
Thanks in advance,
Arun Kumar Srinivasan


Try adding this to your try block:

oWrite.flush();
oWrite.close();

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top