Closing Streams in Java

后端 未结 6 2322
再見小時候
再見小時候 2020-11-27 18:27

Why do we need to close a FileInputStream (and streams in general) in any case before we leave the program? What would happen otherwise? If the program stops before

6条回答
  •  攒了一身酷
    2020-11-27 19:03

    In addition to Jon's answer, it is generally a good idea to close any resource.

    Think of a database connection. Your database cannot have infinite connections opened, in this case when you don't need it, it's better you close it as soon as you're done with it.

    It is also good to make this a habit. "finally" block is your friend. In case of C++, you can also use RAII to manage this automatically.

提交回复
热议问题