Tuesday, October 9, 2007

Last Java Reader Writer Utility you will need (especially for Log Analysis)

It is actually kind of funny, it is amazingly hard to write Java classes/utilities that you can really use over and over again. The requirements of a particular project may change to where code changes to your base library are always required. Your initial library may be too slow or to cumbersome to work with. Whatever the case may be, code-reuse in the Java world is hit or miss in my opinion. Sure it happens, libraries like the Jakarta commons provide useful libraries for common tasks. But, I see way more reuse in functional programming libraries; for example, there are some common data structure libraries that have been in use for a decade. With that being said, I found this first cut at a simple read writer utility reusable.

The goal is simple; given a input file, read the file and then rewrite the output based on that file. I am using this particular code for log file analysis.

Python pseudo code:
f = open(filename)
fw = open(filename, 'w')
data = f.read()
for line in data:

The java version is just that, but I wanted to do the iteration around reading a line through callbacks (anonymous inner subclasses). I designed the following loadFile routine to use an implementation of some ReaderWriter class because you certainly want to control how you iterate through each line of the document and only to focus on that particular task as opposed to the inner details of how the file gets opened or closed. For example:

FileUtil.loadFile(filename, (new HtmlFileUtilReaderWriter()
public void read(BufferedReader bufInput, List resultList) :

while ((feed = bufInput.readLine()) != null

That is basically it, I know it is simple but it is pretty useful idiom. You can download the full source from my googlecode repository. In the future, I hope to create a similar set of libraries in Haskell and compare and contrast between the two languages.



No comments: