LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2006, 12:40 PM   #1
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Rep: Reputation: 32
Java: Reading from a text file slow on Tomcat


I'm using this function to read from a text file:
Code:
	public static String getTextFromFile(String fileName, String encoding)
	throws IOException
	{
		BufferedReader input = new BufferedReader(
		new InputStreamReader(new FileInputStream(
		fileName), encoding));
		String line = input.readLine();
		StringBuilder stringBuilder = new StringBuilder();
		while (line != null)
		{
			stringBuilder.append(line);
			stringBuilder.append("\n");
			line = input.readLine();
		}
		input.close();

		return stringBuilder.toString();
	}
For a file with 3000 lines, this will take 0.25 seconds on my PC (when run as a Java application).
Being run as a Tomcat servlet, this takes minutes...
By debugging the application, I found the part that takes so long on Tomcat is the loop.
I've also tried to use the StringBuffer class.

This is on Java 1.5, Tomcat 5.5.

Any ideas?
Thanks
 
Old 08-22-2006, 01:34 AM   #2
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Original Poster
Rep: Reputation: 32
Hmmm, it became fast again after a reboot... I'm using Eclipse to invoke and start Tomcat (both, in run and debug mode), and apparently, it's getting unreliable (and/or slower) after several starts (even though any Java processes have been ended, properly).

That's what my current findings are...
 
Old 08-22-2006, 04:17 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think reading one line at a time would be rather slow. Furthermore you create and then discard a String object for every loop iteration (might even be 2 String's per loop, I seem to remember using a String literal, the "\n", creates a new String). You might try to read all the bytes at once:
Code:
...
//read in the file in raw byte form
byte[] buf = new byte[fileSize];
fin = new FileInputStream(fileName);
fin.read(buf);
...
return new String(buf, encoding); //translate into encoding
This requires knowing the file size though. There is a more sophisticated method that takes an estimate for file size here.
 
Old 08-23-2006, 01:10 PM   #4
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Original Poster
Rep: Reputation: 32
Thanks ntubski. My goal would always be to make the standard methods work first.
 
Old 08-23-2006, 08:48 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Umm, what do you mean by that exactly?
 
Old 08-24-2006, 11:44 AM   #6
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Original Poster
Rep: Reputation: 32
I mean, the code in my first post is what you can read in any basic Java book, or by searching Google on "java read from text file". And while your code might work - or might not -, that's a rather uncommon, experimental approach. I.e., it contains alot of FIXME comments.
 
Old 08-24-2006, 06:21 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Well, I found that experimental piece of code through Googling actually. Although today I see I was totally on the wrong track; I had read somewhere that read a text file into memory was called "slurping" so I was searching for that, but today when I searched java read file string I see things are much simpler. I somehow missed the available() method in FileInputStream.
So this should work fine:
Code:
...
//read in the file in raw byte form
fin = new FileInputStream(fileName);
byte[] buf = new byte[fin.available()];
fin.read(buf);
...
return new String(buf, encoding); //translate into encoding
Although I really feel that you ought to be able to just attach a FileInputStream to a StringWriter and have all the low-level stuff be taken care automagically, oh well...
 
Old 08-25-2006, 12:07 PM   #8
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Original Poster
Rep: Reputation: 32
Thanks. - However,
Quote:
new FileInputStream(fileName).available()
does not guarantee to return the size of the file it has opened.

I agree, though, that a valid approach was to loop until .available() becomes 0.

- The readLine() approach, in contrast, appears to have severe limitations when no line feed characters exist, and the file is really large.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
Reading Large Text File in C++ maheshrajn Programming 6 03-04-2006 08:54 AM
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration