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: www on 4 Mar 2010 16:54 I remember that there is a way(just one method call) to convert one text file ("msg.txt") their content into one String. For example: msg.txt has the following content: Hello? 123 Good morning I want to get the String "Hello?123Good morning" from it. I know I can do it manually by reading it line by line and concatenate them. But I remember there is a method calling to accomplish this. I searched all the methods in File.java(from Sun) and FileUtils.java(from Apache) and could not find it. Can you help me? Thank you.
From: Eric Sosman on 4 Mar 2010 17:19 On 3/4/2010 4:54 PM, www wrote: > I remember that there is a way(just one method call) to convert one text > file ("msg.txt") their content into one String. For example: > > msg.txt has the following content: > > Hello? > 123 > Good morning > > > I want to get the String "Hello?123Good morning" from it. I know I can > do it manually by reading it line by line and concatenate them. But I > remember there is a method calling to accomplish this. Such a method might exist, but I wouldn't bet on it. The quirk of deleting all the line terminators seems odd, and it's not the sort of behavior I'd expect to find in a general-purpose class. Even without that quirk, "pull the entire file into a string" seems an odd thing to want to do with a file: More commonly, you'd process the file line by line (or chunk by chunk), or you'd load it into something with more structure and "intelligence" than a String. You could, of course, just write whatever loop you like, put it in a method, and call your method. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: markspace on 4 Mar 2010 17:31 www wrote: > msg.txt has the following content: > > Hello? > 123 > Good morning > > > I want to get the String "Hello?123Good morning" from it. I know I can > do it manually by reading it line by line and concatenate them. But I > remember there is a method calling to accomplish this. Not a method no, but a one-liner with the Scanner class might do the trick. Note that this will read all characters, including the newlines and white space, so it won't match exactly your example, but I think it does what you want anyway. String text = new Scanner( new File( "message.txt" ) ) .useDelimiter( "\\$" ).next(); You'll need to throw or catch FileNotFoundException. Don't use \\z or \\Z for the delimiter, it appears to be a bug that it won't read beyond 1024 characters.
From: Roedy Green on 4 Mar 2010 21:03 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 -- Roedy Green Canadian Mind Products http://mindprod.com The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair. ~ Douglas Adams (born: 1952-03-11 died: 2001-05-11 at age: 49)
From: Arne Vajhøj on 4 Mar 2010 22:12
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. Arne |