A small service reads text from a temporary file and passes it on as a string. In the current implementation, reading and closing are linked incorrectly, so the result sometimes depends on the moment the resource is used.
Requirements
- The
SessionReader.loadText method must return the file contents as a String.
- The file must be closed exactly once after reading finishes, even if reading ends with an error.
- External code must not depend on an open resource after the method returns.
- For an empty file, an empty
String must be returned.
Reproduction
- Create a temporary file with several lines.
- Call
SessionReader.loadText(path).
- Use the returned value after leaving the method.
- Check the behavior when an exception occurs during reading and when the file is empty.
Expected behavior
- The returned string always contains the full file text.
- The resource is closed automatically after reading.
- If reading fails, the resource is also closed.
- A repeated call for the same file does not depend on previous reading.