469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Reading a text file into a string in one operation

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
4 60829
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
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
"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
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.

Similar topics

1 post views Thread by fabrice | last post: by
14 posts views Thread by Job Lot | last post: by
8 posts views Thread by Phil Slater | last post: by
4 posts views Thread by Nina | last post: by
4 posts views Thread by Amit Maheshwari | last post: by
1 post views Thread by John | last post: by
4 posts views Thread by chetah | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.