Static analysis

This is a first batch of things a static analysis tool found. These are failures to close files and streams.
Again, you need several try-catch blocks here.