我应该关闭StringReader吗? [英] Should I close a StringReader?
问题描述
我使用 StringReader
将字符串转换为可以上传到SFTP服务器的字符串(它需要一个流)。之后关闭 StringReader
有什么意义吗?据我所知,它只是将字符串设置为 null
...
I use a StringReader
to turn a string into something I can upload to an SFTP server (it takes a stream). Is there any point in closing that StringReader
afterwards? As far as I can see in the source it just sets the string to null
...
我可以这样做,但因为close方法被标记为抛出一个 IOException
而且所有我必须将它包装在try catch中并且代码最终看起来比看起来更糟糕它可能需要。
I could just do it, but since the close method is marked as throwing an IOException
and all I have to wrap it in a try catch and the code just ends up looking a lot more horrible than it perhaps needs to be.
推荐答案
如果你知道你正在处理一个 StringReader
你将丢弃,我认为没有理由关闭它。在你关闭它之后我无法想象你有什么理由持有它的引用,所以对于设置为 null
的字符串来说垃圾没有真正的好处采集。如果您创建的方法需要 Reader
,那么关闭它可能是有意义的,因为您不知道基础类型。
If you know you're dealing with a StringReader
that you'll be throwing away, I don't see any reason to close it. I can't imagine any reason you'd be holding a reference to it after you'd close it, so there's no real benefit to the string being set to null
for garbage collection. If you were creating a method that takes a Reader
then it might make sense to close it since you don't know the underlying type.
这篇关于我应该关闭StringReader吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!