• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading File contents to a String

 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anjali S Sharma
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the above code and it worked fine. Maybe you should check your test file.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic