separating the file writing method to FileUtil