Problem reading large file

ohaya_at_yahoo.com
Date: 04/30/04


Date: 30 Apr 2004 09:23:59 -0700

Hi,

First of all, my apologies if any of you see a similar post in
comp.lang.java. I was rushing, and didn't realize that the
comp.lang.java newsgroup might be deprecated. Hopefully, this is the
correct group now. Also, I don't have access to a news server
directly where I am now, so I'm posting via Google Groups...

Jim

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



Relevant Pages

  • Problem reading large file
    ... I'm a real newbie, but have been asked to try to fix a problem in one of ... StringBuffer fileData = new StringBufferfile.length); ... 'fileData' StringBuffer is created is creating a StringBuffer that is ... having to fix a problem with code from someone else, ...
    (comp.lang.java)
  • Re: Problem reading large file
    ... > 'fileData' StringBuffer is created is creating a StringBuffer that is ... > 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 ... > cause an exception error)? ...
    (comp.lang.java)
  • Re: Problem reading large file
    ... > 'fileData' StringBuffer is created is creating a StringBuffer that is ... I think that a cast of a long to an int just masks the long down to ... it looks like the 'while' loop is NOT size-limiting the reads ...
    (comp.lang.java.programmer)
  • Re: Formatting of strings
    ... 30 chars long. ... resultString = new String; ... the best solution) and proper Java coding. ... However, from a learning Java standpoint, you should look at StringBuffer and StringBuilder classes. ...
    (comp.lang.java.programmer)
  • Re: Why intermediate objects?
    ... >> The one exception is the use of StringBuffer, not StringBuilder, to ... > Doesn't javac produce code with StringBuilder if -target is 1.5+? ... StringBuffer usage ... Prev by Date: ...
    (comp.lang.java.advocacy)