Modified to handle I/O exception so as not to be terminated abnormally due to that the file name which is passed as argument is directory