Added generic methods to write to a file and close all open files.