aspose file tools*
The moose likes Beginning Java and the fly likes Reading File contents to a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading File contents to a String" Watch "Reading File contents to a String" New topic
Author

Reading File contents to a String

Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I am trying to read the file contets to a String. Here's the program I have writteen for the same





What i am trying to do here is read the line to a String called "s" and concatenate whatever "s" has to another String called "contents". Eventually I expected the whole file contents to come into the String called "contents". But the problem is although "s" prints each line correctly "contents" only displays the first line of the file and not the contents of whole file as expected.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Use StringBuffer instead of String. Use append() method on the StringBuffer object to concatenate. With String a new String object is created each time, which is causing the problem.
See the code below ,
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Anjali S Sharma:
I am trying to read the file contets to a String. Here's the program I have writteen for the same





What i am trying to do here is read the line to a String called "s" and concatenate whatever "s" has to another String called "contents". Eventually I expected the whole file contents to come into the String called "contents". But the problem is although "s" prints each line correctly "contents" only displays the first line of the file and not the contents of whole file as expected.


Are you sure that "contents" only contains the first line of the file? How did you verify this? From my analysis of the above code, it looks like it should contain the contents of the whole file but only on a single line. As far as I know, the String returned by readLine() does not contain the end of line character(s), this means that "contents" won't contain any "\n" characters so it will look like it is all one line. With that said, it still should contain the whole file.

All this means is that you need to manually append a "\n" each time you add a line to "contents".

I would also like to reiterate that you should use StringBuffer (or if you are using JDK 1.5, StringBuilder is preferred). This avoids the memory overhead of creating many, many String objects as your current code does. I suggest that you google for more information on String so that you can understand how memory is allocated when you create String objects.

HTH

Layne


Java API Documentation
The Java Tutorial
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Thanks everyone for replying.
Originally posted by Layne Lund:
[CODE]

Are you sure that "contents" only contains the first line of the file? How did you verify this?
HTH

Layne


I verified by printing it and it prints only the first line again and again.
I agree that StringBuffer should be used and not String as it'll cause a lot of overhead.
But keeping the overhead thing aside for a while, why doesn't this code works ? I can't find a reason for this not working.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

I ran the above code and it worked fine. Maybe you should check your test file.


[How To Ask Questions On JavaRanch]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

And if you're doing this for a reason other than an academic exercise, you can spare yourself the trouble of reinventing the wheel ("spare", "wheel"... get it?) by using the Jakarta Commons IO package. There's a nifty method called FileUtils.readFileToString() that sounds like just what you're looking for...

Regards...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading File contents to a String