Problem reading large file
From: ohaya (ohaya_NO_SPAM_at_NO_SPAM_cox.net)
Date: 04/30/04
- Next message: Bernard Koninckx: "Errors when deleting a row in a JTable."
- Previous message: Andrew Swallow: "Re: Cracking DES with C++ is faster than Java?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Apr 2004 10:50:42 -0400
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
- Next message: Bernard Koninckx: "Errors when deleting a row in a JTable."
- Previous message: Andrew Swallow: "Re: Cracking DES with C++ is faster than Java?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|