Re: How to convert one text file into one large String?
On Sat, 6 Mar 2010, Eric Sosman wrote:
On 3/6/2010 7:00 AM, Tom Anderson wrote:
On Fri, 5 Mar 2010, Eric Sosman wrote:
[...]
It also assumes that f.length() returns the file's length. It will do
so if it can, but it may not be possible: Suppose the "file" is a
keyboard, or a pipe, or a socket, or something else whose length won't
be known until after all the data has been read.
Yes, then you're in trouble.
In such cases, f.length() will return 0L ...
Seriously?
No, not really. The Javadoc and I are just having our
little joke.
Oh, those zany javadocs!
That seems a very broken thing to do to me.
What would you suggest it should return instead?
There is no right answer for it to return, so it has to do what a method
does when it can't complete successfully: throw an exception.
-1L might be a plausible choice, but your `new byte[f.length()]' would
run into trouble. Throwing a HowShouldIKnowException might be another
possibility, but once again the way you've called the method would be
open to question.
Absolutely. My code can't possibly work if the length of the file isn't
known ahead of time, so again, the right thing for it to do is blow up
with an exception. Having File.length() return -1 or throw an exception of
its own would achieve that. Returning 0 allows code written by lazy
programmers, such as exhibit A above, to fail silently, which is bad.
tom
--
I really don't know what any of this shit means, but it looks
impressive. -- zerolives, on YVFC