completed writeInfile method