Prev: How to get the file name from a String containing the path plus the file name?
Next: CodeWarrior begins on Mar 5, 06:00 pm IST. Certificates & Prizes worth 20,000 INR.
From: Tom Anderson on 5 Mar 2010 13:27 On Thu, 4 Mar 2010, Arne Vajh?j wrote: > On 04-03-2010 21:03, Roedy Green wrote: >> On Thu, 04 Mar 2010 16:54:59 -0500, www<www(a)nospam.com> wrote, quoted >> or indirectly quoted someone who said : >>> I searched all the methods in File.java(from Sun) and >>> FileUtils.java(from Apache) and could not find it. >> >> see http://mindprod.com/products1.html#HUNKIO >> for the source code > > Given that a one liner using only standard Java API has been posted, > then there are no need. Just for fun, here's a two-liner, where f is a File: byte[] buf = new byte[(int)f.length()]; new DataInputStream(new FileInputStream(f)).readFully(buf); tom -- A TEN PINTS
From: Tom Anderson on 5 Mar 2010 13:31 On Fri, 5 Mar 2010, Tom Anderson wrote: > On Thu, 4 Mar 2010, Arne Vajh?j wrote: > >> On 04-03-2010 21:03, Roedy Green wrote: >>> On Thu, 04 Mar 2010 16:54:59 -0500, www<www(a)nospam.com> wrote, quoted >>> or indirectly quoted someone who said : >>>> I searched all the methods in File.java(from Sun) and >>>> FileUtils.java(from Apache) and could not find it. >>> >>> see http://mindprod.com/products1.html#HUNKIO >>> for the source code >> >> Given that a one liner using only standard Java API has been posted, then >> there are no need. > > Just for fun, here's a two-liner, where f is a File: > > byte[] buf = new byte[(int)f.length()]; > new DataInputStream(new FileInputStream(f)).readFully(buf); Although of course that gets you a byte array, not a String. My bad. The full answer is a three-liner: String s = new String(buf, "UTF-8"); // or whatever charset you like Although this involves using N bytes of memory for the buffer and up to 2N bytes for the string at the same time, if only briefly. There should be a way to do it that just involves storing 2N bytes, and perhaps Scanner does this. tom -- A TEN PINTS
From: Eric Sosman on 5 Mar 2010 16:32 On 3/5/2010 1:31 PM, Tom Anderson wrote: > On Fri, 5 Mar 2010, Tom Anderson wrote: > >> On Thu, 4 Mar 2010, Arne Vajh?j wrote: >> >>> On 04-03-2010 21:03, Roedy Green wrote: >>>> On Thu, 04 Mar 2010 16:54:59 -0500, www<www(a)nospam.com> wrote, quoted >>>> or indirectly quoted someone who said : >>>>> I searched all the methods in File.java(from Sun) and >>>>> FileUtils.java(from Apache) and could not find it. >>>> >>>> see http://mindprod.com/products1.html#HUNKIO >>>> for the source code >>> >>> Given that a one liner using only standard Java API has been posted, >>> then there are no need. >> >> Just for fun, here's a two-liner, where f is a File: >> >> byte[] buf = new byte[(int)f.length()]; >> new DataInputStream(new FileInputStream(f)).readFully(buf); > > Although of course that gets you a byte array, not a String. My bad. The > full answer is a three-liner: > > String s = new String(buf, "UTF-8"); // or whatever charset you like > > Although this involves using N bytes of memory for the buffer and up to > 2N bytes for the string at the same time, if only briefly. There should > be a way to do it that just involves storing 2N bytes, and perhaps > Scanner does this. 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. In such cases, f.length() will return 0L ... -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Tom Anderson on 6 Mar 2010 07:00 On Fri, 5 Mar 2010, Eric Sosman wrote: > On 3/5/2010 1:31 PM, Tom Anderson wrote: >> On Fri, 5 Mar 2010, Tom Anderson wrote: >> >>> On Thu, 4 Mar 2010, Arne Vajh?j wrote: >>> >>>> On 04-03-2010 21:03, Roedy Green wrote: >>>>> On Thu, 04 Mar 2010 16:54:59 -0500, www<www(a)nospam.com> wrote, quoted >>>>> or indirectly quoted someone who said : >>>>>> I searched all the methods in File.java(from Sun) and >>>>>> FileUtils.java(from Apache) and could not find it. >>>>> >>>>> see http://mindprod.com/products1.html#HUNKIO >>>>> for the source code >>>> >>>> Given that a one liner using only standard Java API has been posted, >>>> then there are no need. >>> >>> Just for fun, here's a two-liner, where f is a File: >>> >>> byte[] buf = new byte[(int)f.length()]; >>> new DataInputStream(new FileInputStream(f)).readFully(buf); >> >> Although of course that gets you a byte array, not a String. My bad. The >> full answer is a three-liner: >> >> String s = new String(buf, "UTF-8"); // or whatever charset you like >> >> Although this involves using N bytes of memory for the buffer and up to >> 2N bytes for the string at the same time, if only briefly. There should >> be a way to do it that just involves storing 2N bytes, and perhaps >> Scanner does this. > > 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? That seems a very broken thing to do to me. tom -- limited to concepts that are meta, generic, abstract and philosophical -- IEEE Standard Upper Ontology Working Group
From: Eric Sosman on 6 Mar 2010 08:18
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. > That seems a very broken thing to do to me. What would you suggest it should return instead? -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. -- Eric Sosman esosman(a)ieee-dot-org.invalid |