try-with-resources / use / multiple resources

前端 未结 4 1788
臣服心动
臣服心动 2021-02-20 01:07

I\'m using a Java-API which heavily uses the Autoclosable-Interface and thus in Java try-with-resources. However in Java you can specify



        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 01:45

    There is no standard solution for this. If you had all of the Closable instances ready at the start, you could use your own self-defined methods to handle them, like this blog post or this repository shows (and here is the discussion on the official forums that led to the latter).

    In your case however, where subsequent objects rely on the previous ones, none of these apply like a regular try-with-resources would.

    The only thing I can suggest is trying to define helper functions for yourself that hide the nested use calls, and immediately place you in the second/third/nth layer of these resourcs acquisitions, if that's at all possible.

提交回复
热议问题