When does a stream close if its not closed manually?

后端 未结 6 2083
一个人的身影
一个人的身影 2021-01-04 10:27

I would like to know when does a stream close if its not closed manually. By this I mean, will the stream be closed if the scope of its reference is no more?

Conside

6条回答
  •  盖世英雄少女心
    2021-01-04 11:16

    In the case of FileInputStream there's a finalize() method that will free resources when the stream is garbage collected.

    Not that you can or should rely on that. It's just what FileInputStream does. SocketInputStream on the other hand overrides finalize() to explicitly do nothing, relying on Socket to close the resources (and Socket doesn't have a finalize() method).

提交回复
热议问题