By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Problem reading large file

P: n/a
Hi,

I'm a real newbie, but have been asked to try to fix a problem in one of
our JSP pages that is suppose to read in a text file and display it.

From my testing thus far, it appears this page is somehow hanging when
relatively large file is used.

My original intent was to try to just add a check for file size, and
error out somehow if the file was "too" large, but in looking at the
code, I'm not quite sure what might cause it to hang even with large
files.

Here's a portion of the code, with some of the code, etc. removed for
clarity and with some comments (and hoping that I don't make any typos):

..
..
String finalFileData = null;
..
..
java.io.File file = new java.io.File (filePath); // filePath is a
String, containing full path+name
..
..
try {
java.io.FileReader freader = null;
try {
freader = new java.io.FileReader(file); // create FileReader
StringBuffer fileData = new StringBuffer((int) file.length());
char[] chars = new char[SOME_BUFFER_SIZE];
int c = freader.read(chars); // read initial buffer into 'chars'
while (c > 0) {
fileData.append(chars, 0, c); // append string from 'chars' to
'fileData'
c = freader.read(chars); // read next buffer into 'chars'
}
finalFileData = fileData.toString();
} finally {
if (freader != null)
freader.close();
}
} catch (Exception e) {
finalFileData = null;
}
..
..

When I looked at the code above, it seems like the the line where the
'fileData' StringBuffer is created is creating a StringBuffer that is
size-limited to the maximum of an int in Java. I'm assuming that this
limit is 'Integer.MAX_VALUE'. Is that correct?

Assuming that that's (that the fileData size is set that way) the case,
it looks like the 'while' loop is NOT size-limiting the reads into the
fileData StringBuffer, so I'm guessing that that is what is the
problem.

Does this seem about right?

Then, I don't understand why the page is just hanging, and not
generating an exception. Shouldn't the try (and catch, at the end)
cause an exception error)?

As I mentioned above, I'm kind of a real newbie with Java/JSP, and
having to fix a problem with code from someone else, so I hope that this
is not too stupid question.

Thanks in advance,
Jim
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Why not use a java.io.BufferedReader to read in the text file? It
will make your code a lot simpler, and the file will be read in more
quickly.
- Glenn
ohaya <ohaya_NO_SPAM@NO_SPAM_cox.net> wrote in message news:<409267C2.D2C98263@NO_SPAM_cox.net>...
Hi,

I'm a real newbie, but have been asked to try to fix a problem in one of
our JSP pages that is suppose to read in a text file and display it.

From my testing thus far, it appears this page is somehow hanging when
relatively large file is used.

My original intent was to try to just add a check for file size, and
error out somehow if the file was "too" large, but in looking at the
code, I'm not quite sure what might cause it to hang even with large
files.

Here's a portion of the code, with some of the code, etc. removed for
clarity and with some comments (and hoping that I don't make any typos):

.
.
String finalFileData = null;
.
.
java.io.File file = new java.io.File (filePath); // filePath is a
String, containing full path+name
.
.
try {
java.io.FileReader freader = null;
try {
freader = new java.io.FileReader(file); // create FileReader
StringBuffer fileData = new StringBuffer((int) file.length());
char[] chars = new char[SOME_BUFFER_SIZE];
int c = freader.read(chars); // read initial buffer into 'chars'
while (c > 0) {
fileData.append(chars, 0, c); // append string from 'chars' to
'fileData'
c = freader.read(chars); // read next buffer into 'chars'
}
finalFileData = fileData.toString();
} finally {
if (freader != null)
freader.close();
}
} catch (Exception e) {
finalFileData = null;
}
.
.

When I looked at the code above, it seems like the the line where the
'fileData' StringBuffer is created is creating a StringBuffer that is
size-limited to the maximum of an int in Java. I'm assuming that this
limit is 'Integer.MAX_VALUE'. Is that correct?

Assuming that that's (that the fileData size is set that way) the case,
it looks like the 'while' loop is NOT size-limiting the reads into the
fileData StringBuffer, so I'm guessing that that is what is the
problem.

Does this seem about right?

Then, I don't understand why the page is just hanging, and not
generating an exception. Shouldn't the try (and catch, at the end)
cause an exception error)?

As I mentioned above, I'm kind of a real newbie with Java/JSP, and
having to fix a problem with code from someone else, so I hope that this
is not too stupid question.

Thanks in advance,
Jim

Jul 17 '05 #2

P: n/a


Glenn wrote:

Why not use a java.io.BufferedReader to read in the text file? It
will make your code a lot simpler, and the file will be read in more
quickly.

- Glenn

Glenn,

Thanks for your response. I did try that, but it didn't help, and
actually, it turns out that the problem appears to be some kind of
problem in IE6.

What I didn't show in my code snippet was that after the file is
completely read into the String, we output the result inside a
TEXTAREA. It turns out that the original person who coded the page had
included a "style" attribute in the TEXTAREA, with width and height of
'0px' (there's some other code to re-size the TEXTAREA), and it looks
like IE6 chokes when you try to output large content within such a
TEXTAREA, and when width/height is 0px. I changed the height/width to
'1px', and things were working again.

Again, thanks,
Jim
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.