Simplified code by using Apache Commons IOUtils