473,624 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FileRea der freader = null;
try {
freader = new java.io.FileRea der(file); // create FileReader
StringBuffer fileData = new StringBuffer((i nt) file.length());
char[] chars = new char[SOME_BUFFER_SIZ E];
int c = freader.read(ch ars); // read initial buffer into 'chars'
while (c > 0) {
fileData.append (chars, 0, c); // append string from 'chars' to
'fileData'
c = freader.read(ch ars); // read next buffer into 'chars'
}
finalFileData = fileData.toStri ng();
} 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_VA LUE'. 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 12975
Why not use a java.io.Buffere dReader 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_SPA M_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.FileRea der freader = null;
try {
freader = new java.io.FileRea der(file); // create FileReader
StringBuffer fileData = new StringBuffer((i nt) file.length());
char[] chars = new char[SOME_BUFFER_SIZ E];
int c = freader.read(ch ars); // read initial buffer into 'chars'
while (c > 0) {
fileData.append (chars, 0, c); // append string from 'chars' to
'fileData'
c = freader.read(ch ars); // read next buffer into 'chars'
}
finalFileData = fileData.toStri ng();
} 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_VA LUE'. 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.Buffere dReader 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
4741
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 class I define. It works perfectly on an old machine (PII-400) running Python 2.2.1 under RedHat Linux 8.0. When I try to run it under Python for windows ME on a P-4 1.4 GHz, however, it keeps crashing on reading from the shelved file the second...
7
3692
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... so I have to try to "hardcode" IE to display any webpage by a scale of 200%...
20
33031
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. From here read a character, print it, move the file pointer (FILE*) to 2 steps back (using fseek(fp, -2, SEEK_CUR)) to read the previous character. This seems to be ok if the file has a single line (i.e. no new line character). The above logic...
6
6337
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
9
35263
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 streamReader = new StreamReader(stream, Encoding.ASCII)) { string line = ""; DateTime currentDate = DateTime.Now.Date; while (streamReader.Peek() > -1)
3
2063
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 in a file in a single Read and so my quesitons are: (1) What is a practical file size limit for reading using FileStream.Read (reading the file in a single read)...especially on a web server where I don't think I'd want to tax memory...
7
4030
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 creates a bunch of PDFs with Crystal Reports. Another service is a remote object which serves as our data access component -- basically it just executes stored procedures and returns datasets. If you watch the services in task manager, you can...
5
14980
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
3
2940
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 after loading I've looked at most of the samples on the web in using GDI+... they all fall over dead with these large files. Any help is appreciated.
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8330
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.