473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem reading large file

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
2 12960
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rami A. Kishek | last post by:
Hi - this mysterious behavior with shelve is just about to kill me. I hope someone here can shed some light. First of all, I have this piece of code which uses shelve to save instances of some...
7
by: gino | last post by:
Dear all, My monitor was set to 1600x1200 so the fonts in IE is too small for me even when I set the "View->Text Size->Largest"... I don't have previlage to change the monitor resolution... ...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
9
by: haibhoang | last post by:
I have a Windows Service that is trying to parse a large (> 1Gig) text file. I am keep getting OutOfMemoryException exception. Here is the code that's having problem: using (StreamReader...
3
by: Brad | last post by:
I'm working on a web app which will display LARGE tiff image files (e.g files 10-20+ mb). Files are hidden from users direct access. For other, smaller image files I have used FileStream to read...
7
by: HeatherS | last post by:
We are having issues with our windows services using memory and never releasing it. We have one service that has a file watcher which takes an xml file, inserts some records into a database, and...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
3
by: =?Utf-8?B?dGtpZWhs?= | last post by:
I have large 1bpp tiff scans of arch. drawings that are typically 12032x16890 pixels (filesize is about a 1 meg +/-) While I can readily view smaller (dimension) files, when I try to do anything...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.