Close file streams to avoid fd leakage

- Added close of file streams in finally blocks to avoid filedescriptor leaks
- Removed unused imports
- Removed unneeded semicolon