venerdì 8 ottobre 2010

Just a little bit about Files.....

Some time (many times), you need to deal with files.....
One important thing to remembar when you manage files in Java is that the object "File" (java.io.File) is not a fisical file but a reference to a potential not necessary existing file.
When you declare a File object as in :

File fp = new File("nomeFile.txt");

You are not "opening" the file "nomeFile.txt", but preparing yourself to deal with a file (or bettar a file system object) whos name should be that.
Infact the file could still not be there, or it could be something different from a file (a directory? yes! but evene other objects).

Take a look at the following code:


import java.io.*;

public class FileTest {
  public static void main(String args[]) {
    File f = new File(args[0]); 
String msg = "";
    if (f.exists())
       msg = " is found";
    else
       msg = " does not exists";
System.out.println(f + msg);
  }
}

This code explains that when you create a File object you may not yet be handling the file itself, and you can manage the existence of it.
Have fun guys !

mercoledì 15 settembre 2010

Log log log and always log !

No maters which tool you use, but you MUST log !

If you develop an application of more then 3 lines of code (and do not participate to a Java Compact Code Competition), at least one every 5 should be a log line.

The worst parts of programming are documenting and then logging, specially because most time we add add and add logging during the troubleshooting of bugs, but then we never rationalize them and so thousands of useless logs are generated and the really useful few ones got lost in an ocean of noise.

The core of a good logging is the choose of the right detail and information you put into the messages, and of a tool that enable you to easily  activate and set the needed level at runtime with out overloading too much the execution of the application.

As almost all the technical works you must find the right equilibrium between too much and too few, and you can only use your thumb as a meter.

But you MUST always keep in your mind the good old rule:
IF YOU DO NOT LOG YOU’LL NEVER KNOW WHAT YOU ARE DOING WRONG !

venerdì 10 settembre 2010

Errors and Exceptions....

Seems a simple argument, but often junior programmers ignore that there are Errors!
Exception handling mechanism is well known, but the fact that Exceptions are one of the two separate sub-groups of the Throwable class is not often mastered.
The result is that often we catch exceptions but ignore errors.
Is just a little thing we have to keep in mind, mainly if your programs execute inside complex environmets.
Infact an unmanaged error could cause the whole JVM to close while you think that you have managed all the possible exceptions.

Yes: you managed all possible exceptions, but not a single error ! ;)

But there is a good news for all of you folks : Errors are exactly as Exceptions, so you can manage them the same way !

So why do we have two different groups ?
Becouse Exceptions are things you should not make to happen but must manage inside of you, while Errors are lightings hitting you when you can't do much to protect yourself.... as OutOfMemoryError !

But no metter the difference between the two group, let's manage even Errors, it's not so difficult after all:

try
{
   ....
}
catch(Exception ex)
{
  ....
}
catch (Error er)
{
  ...
}

Have a lot of fun with your JavaCode!

venerdì 3 settembre 2010

Welcome to my Java Experience Blog

Welcome to my Java Experience Blog, where I'd like to share what I've learned in many years of  Java Development.

I hope you'll find some good tricks and why not even some usefull source code.

Keep connected and maybe I'll try to even add something you ask for, taking a Java Rabbit out of my hat !