Chitika

Tuesday, May 17, 2005

recursive directory search

A friend of mine just asked me how to write a Java code that will perform a search on a directory and all of its subdirectories (recursively) matching a given file suffix, for example, all JSP files. I remembered that I was doing a small project a few months ago that had to deal with this functionality. And, I also remembered that it was a simple thing to do.

So I went to the jakarta-commons site, looking for the commons-io component. After looking at the javadoc for the next few seconds, suddenly it's all there. The solution is all there. Just one line of code, and you're done.

Collection jspFiles = FileUtils.listFiles
    (rootDirName, new String[] { "jsp" }, true);

I can't keep myself from the amusement caused by the great work that open source people have done. This is yet another prove that a lot of browsing could actually prove beneficial.. :D
It's nice to see the benefits of open source components. I'd certainly went and looked for jakarta-commons next time I'd ever need anything "common".. :D

Kudos to the great guys everywhere in the open source world.. :D

Further Reading:
Jakarta Commons Cookbook
Pro Jakarta Commons
Apache Jakarta Commons
Applied Software Engineering using Apache Jakarta Commons

Thursday, April 28, 2005

String bug

Apparently, there is a bug in the Sun JDK 1.4.2 or lower implementation of java.lang.String. This bug has been identified as 4310930, 4546734, 4637640, 4724129 in Sun's Bug Database. I've come across this bug while reading Charles Miller's post, when he was having trouble with the OutOfMemoryError in his JDBC code.

Please download this source code to see what I'm talking about. Try to run the source code, and continue until you reach the OutOfMemoryError state, as seen in my own trial here. Then, change the source code to the working alternatives, and test them out (see my result here).

This bug has been fixed in JDK 5.0 release, but I believe most of us are still working with Sun JDK 1.4.2 or lower. So I guess this is a problem that we must be very careful with.

Thursday, April 14, 2005

Petals around the Rose Puzzle

A friend of mine send me a link to the Petals around the Rose Puzzle. It's essentially a simulation of rolling 5 dice at the same time, then guess what is the number of Petals around the Rose. It's quite fun actually to guess them. I've had 16 tries (approx. 15 mins) before finding my solution to the problem. The solution has been proven within the next 30 successfull tries.

Try them out for yourself, and let me know how you did it.

Here's my solution in Spanish: (use Google Translator to translate them into English)
No haga caso de todo, a excepción de 5 y de 3. Cada 5 cuentas para 4 pétalos. Cada 3 cuentas para 2 pétalos. Resuma el número de pétalos.

Another friend of mine come up with a smarter (more visual) solution, also within 10-15 minutes time. Here's his solution in Spanish:
La rosa es el punto en el centro de el corto en cubos. Solamente 1/3/5 tiene ese punto. Por lo tanto, 2/4/6 no es se levantó. El pétalo es los otros puntos que rodean color de rosa. Así, solamente 3/5 rosa tiene 2/4 de los pétalos.

Let me know your solution.. :D
It's just a fun way to start the day.. :D