You can do this automatically if you put the close()-method call into the finalize()-method of this class.