Monday, August 10, 2009

The Big Gulp Method

Movie Review:

Julie and Julia

"Most strikingly, this is a Hollywood movie about women that is not about the desperate pursuit of men."

This blogger is NOT a moviegoer. For a movie about Julia Child, I make an exception. Mastering the Art of French Cooking was one of my bibles. Child's shows on PBS were my inspiration. Over the years my cooking has moved south: olive oil has replaced butter, seafood has replaced beef. Still, it all started with Julia.

The Big Gulp Method:

One of the Java-beginner articles on my website suggested that reading a file using Java's streamIO, as was universally suggested in every book I ever saw (my own excluded), was slow, foolish, and too much work for programmer and computer alike.

This was all that you needed:


RandomAccessFile raf = new RandomAccessFile
( "C:/mrwebsite/articles/raf.html", "r" );
bytes = new byte[ (int) raf.length() ];
raf.read( bytes );
raf.close();


In Python, that simplifies to:


file = open( pathname )
arr = file.read()
file.close()


In Python you can also use file.readlines() to populate an array with a text file, one line per array element. This feature is also available in Ruby with near-identical syntax and identical simplicity.

Of course, all of the above is simpler than file I/O in a finished application. Whatever the language, you have to try the file input (does the file exist?), catch errors and provide options for the user to recover from the errors.

Still, the big-gulp method of reading the file into a byte array (or line array, for text) is fast and easy.

Typical Tutorial:

So why do we still see this?


a = open("sample.txt", "r")
line = a.readline()
while line:
    print line
    line = a.readline()
a.close()


That bit happens to be from about.com, but it's typical of what is given to beginners. Beginners do as they are shown and we are doing file I/O in a way that made sense when RAM size was measured in kilobytes, not gigabytes. And so it goes.

No comments:

Post a Comment