Use try-with-resources.

Since the build depends on Java 7 now, use Java 7 language features
to simplify the code.