Use try with resource when possibile to ensure streams are closed properly