在 Java 中关闭流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/515975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing Streams in Java
提问by user42155
Why do we need to close a FileInputStream (and streams in general) in any casebefore we leave the program? What would happen otherwise? If the program stops before the input stream is closed explicitly in the program, doesn't the stream also close automatically?
我们为什么需要关闭一个FileInputStream(和溪流一般)在任何情况下,我们离开这个程序之前?否则会发生什么?如果程序在程序中显式关闭输入流之前停止,流是否也会自动关闭?
回答by Jon Skeet
Yes, when the process terminates the unmanaged resources will be released. For InputStreams this is fine. For OutputStreams, you could lose an buffered data, so you should at leastflush the stream before exiting the program.
是的,当进程终止时,非托管资源将被释放。对于 InputStreams 这很好。对于 OutputStreams,您可能会丢失缓冲数据,因此您至少应该在退出程序之前刷新流。
回答by Srikanth
In addition to Jon's answer, it is generally a good idea to close any resource.
除了 Jon 的回答之外,关闭任何资源通常是一个好主意。
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.
养成这个习惯也很好。“终于”块是你的朋友。在 C++ 的情况下,您还可以使用RAII 自动管理它。
回答by Joao da Silva
Sort of. The stream is backed by a "real" operating system file descriptor, and when the process terminates the OS will clean up any open file descriptors.
有点。该流由“真实”操作系统文件描述符支持,当进程终止时,操作系统将清除所有打开的文件描述符。
Anyway, it's good practice to always close your resources when you're done, so close the streams :)
无论如何,完成后始终关闭资源是一种很好的做法,因此请关闭流:)
回答by duffymo
File handles are scarce, finite resources. You can run out of them if you don't clean them up properly, just like database connections.
文件句柄是稀缺的、有限的资源。如果您没有正确清理它们,您可能会耗尽它们,就像数据库连接一样。
If you've written a small program with just one user you can get away with being sloppy and not closing in a finally block.
如果你编写了一个只有一个用户的小程序,你可以避免草率而不是在 finally 块中关闭。
But if you end up using that idiom in an application that has many users and file handles you might have a problem.
但是,如果您最终在具有许多用户和文件句柄的应用程序中使用该习惯用法,您可能会遇到问题。
"First we make our habits, then they make us." I try to apply best practices even when they aren't necessary.
“首先我们养成习惯,然后习惯养成我们。” 即使没有必要,我也会尝试应用最佳实践。
回答by Philip Reynolds
If you don't close streams, you may have problems opening them back up again. This is especially true if they're hanging off the end of sockets.
如果您不关闭流,您可能会在再次打开它们时遇到问题。如果它们挂在套接字的末端,则尤其如此。
Closing a stream also makes sure that data is flushed through the stream if there is any data left to send.
如果还有任何数据要发送,关闭流还可以确保数据通过流刷新。
回答by Don Branson
Dude. If you don't close your stream, your unit test will fail. Or at least, it should. So, that's why you need to close it. ;)
伙计。如果您不关闭流,您的单元测试将失败。或者至少,它应该。所以,这就是你需要关闭它的原因。;)
And while the OS will almost certainly clean up if you just exit, they'll generally get freed up faster if you explicitly close them. Furthermore, what if your code ends up in a long-running program sometime down the road? Then they'll have problems and curse you. :(
虽然如果您退出操作系统几乎肯定会进行清理,但如果您明确关闭它们,它们通常会更快地被释放。此外,如果您的代码在某个时候最终出现在一个长时间运行的程序中怎么办?然后他们会遇到问题并诅咒你。:(
So, it's like washing your hands after using the bathroom. Eventually someone will pay the price if you don't do it. You can get away with it for a while, but it's still a good practice.
所以,这就像上完厕所后洗手。如果你不这样做,最终会有人付出代价。你可以暂时摆脱它,但这仍然是一个很好的做法。