Added try-with-resources statement for read() and flush() methods