By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

Reading a text file into a string in one operation

P: n/a
Is there a neat way to read the entire contents of a file into a string
without having to write a read() loop each time? I would have thought
there would be some stream that has a method that lets you read the
entire stream into a string with one line of code, but haven't been able
to find one. Now, this is pretty easy to write myself, but it would be
nicer to use some standard method for such a fundamental operation...

Rune
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Perhaps this might help you out
its using what you want to avoid but show you how to make a method for it:
http://www.javazoid.com/foj_file.html

On Mon, 17 May 2004 00:27:48 +0200, Rune Berge <ru**@krokodille.com> wrote:
Is there a neat way to read the entire contents of a file into a string
without having to write a read() loop each time? I would have thought
there would be some stream that has a method that lets you read the
entire stream into a string with one line of code, but haven't been able
to find one. Now, this is pretty easy to write myself, but it would be
nicer to use some standard method for such a fundamental operation...

Rune


--
// Daniel Jensen Aka. Daniboy
Da******@456believeit131hotmail.com
Jul 17 '05 #2

P: n/a
Daniel Jensen wrote:
Perhaps this might help you out
its using what you want to avoid but show you how to make a method for it:
http://www.javazoid.com/foj_file.html


Thanks, but I know how to make a method for it. I just wanted to know
whether such a method already exists. However, the article states that
such a method does, in fact, NOT exist, so you indirectly answered my
question anyway :)
Rune
Jul 17 '05 #3

P: n/a
"Rune Berge" <ru**@krokodille.com> wrote in message
news:0T**********************@news000.worldonline. dk...
Is there a neat way to read the entire contents of a file into a string
without having to write a read() loop each time? I would have thought
there would be some stream that has a method that lets you read the
entire stream into a string with one line of code, but haven't been able
to find one. Now, this is pretty easy to write myself, but it would be
nicer to use some standard method for such a fundamental operation...

Rune


If you can guarantee the maximum length of the file or don't mind using
absurdly large buffers (and possibly increasing the maximum heap size) you
could do some type of buffered read.
Jul 17 '05 #4

P: n/a
Bob

"Rune Berge" <ru**@krokodille.com> wrote in message
news:0T**********************@news000.worldonline. dk...
Is there a neat way to read the entire contents of a file into a string
without having to write a read() loop each time? I would have thought
there would be some stream that has a method that lets you read the
entire stream into a string with one line of code, but haven't been able
to find one. Now, this is pretty easy to write myself, but it would be
nicer to use some standard method for such a fundamental operation...

in a word. Perl.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.