If it safe to return an InputStream from try-with-resource [duplicate]
问题 This question already has answers here : Try-with-resources and return statements in java (4 answers) Closed 5 years ago . Is it safe to return an input stream from a try-with-resource statement to handle the closing of the stream once the caller has consumed it? public static InputStream example() throws IOException { ... try (InputStream is = ...) { return is; } } 回答1: It's safe, but it will be closed, so I don't think it is particularly useful... (You can't reopen a closed stream.) See