Do not open and close the file stream of the output file each time you write one line. Instead open it once at the beginning and close it at the end (open/close a file stream is an expensive operation).
Put all the code in a try finally block. Close all file streams in the finally block to ensure that the streams will be closed if an exception occured.