Reading a File in Java -- Beautifully
Thursday, October 25, 2007 9:16:32 AM
Till today, I used to read an entire text file like this:
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(newLine);
}
return sb.toString();
That's not beautiful. But today I came up with this alternative:
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
for (String line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line).append(newLine);
}
return sb.toString();
And I like its beauty. What do you think? Isn't this more beautiful?
It would be more convenient if BufferedReader had a readFile() method though. Also it makes sense to make BufferedReader iterable so we could have:
for (String line : buffReader) { // do something with line }







Unregistered user # Thursday, October 25, 2007 12:10:37 PM
Unregistered user # Thursday, October 25, 2007 2:31:33 PM
Unregistered user # Thursday, October 25, 2007 3:04:00 PM
Behrang Saeedzadehbehrangsa # Thursday, October 25, 2007 5:23:52 PM
That makes sense, but every now and then that I have to experiment with something or write a tiny small project (say, less than 1000 lines), if I need to read form a file, I usually do that manually. It is not that cumbersome after all. I don't know why I hesitate to create my common utilities JAR file. Oh, btw, Commons IO has this neat readFileToString() method but again, I refuse to use extra libs for my small projects. Maybe I prefer this approach, because in general, I like to have a minimal dependency on external JAR files. Maybe I have to rethink about this attitude of mine thought.
One cool thing to do is to write a code snippet template in IDEA or Eclipse , say named rdfl (for readfile), and then by expanding it, it would insert the above chunk of code (something a little bit more complex) at caret position.
@Anonymous,
That was even more beautiful, but I assume more resource hungry, which is of course not important at all for a small experimental project.
Unregistered user # Thursday, October 25, 2007 8:23:48 PM
Behrang Saeedzadehbehrangsa # Thursday, October 25, 2007 9:48:43 PM
Almost everywhere, I have seen people using the while-loop approach for reading files. This leaves a line variable in a broader scope than it is necessary to be. Of course, this peice of code is usually wrapped inside a method. But again, IMO, the for loop is more beautiful than the while loop. Trivial but worthy nonetheless.
Unregistered user # Saturday, October 27, 2007 9:39:50 PM
Unregistered user # Saturday, October 27, 2007 9:40:51 PM
Behrang Saeedzadehbehrangsa # Saturday, October 27, 2007 10:57:48 PM
Looks like Opera is facing some issues regarding the formatting of comments for the time being: http://my.opera.com/community/forums/topic.dml?id=210362 but your LineReader was interesting.
Unregistered user # Saturday, December 1, 2007 6:30:00 PM
Unregistered user # Friday, January 4, 2008 12:42:51 AM
Unregistered user # Monday, January 28, 2008 5:25:51 AM
Unregistered user # Tuesday, February 19, 2008 10:32:23 PM
Unregistered user # Monday, July 27, 2009 4:53:57 PM
Unregistered user # Monday, May 10, 2010 3:04:20 PM