构造 BufferedReader,以 BufferedReader 作为读取器
我可以做这个科目吗?我的意思是:
BufferedReader reader1 = new BufferedReader(new FileReader(new File("file")));
BufferedReader reader2 = new BufferedReader(reader1);
如果我在这种情况下尝试使用 BufferedReader (第二个)会发生什么?它是正确的吗?
Can I do this subj? I mean:
BufferedReader reader1 = new BufferedReader(new FileReader(new File("file")));
BufferedReader reader2 = new BufferedReader(reader1);
What will happen if I'll try to use BufferedReader (second one) in this case? Does it is correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从它可以工作的意义上来说,它是正确的:
您可以从任何有效的 Reader,甚至另一个 BufferedReader 构造一个 BufferedReader。
然而,它不会真正提高性能或产生任何其他有益效果。您应该只使用
reader1
而不是创建第二个。如果您开始混合调用
reader1
和reader2
,它甚至可能会产生负面影响,但这只是Java中流的一般概念:一旦您包装流(或读取器/写入器),您不应再直接访问它。It is correct in the sense that it will work:
You can construct a
BufferedReader
from any validReader
, even anotherBufferedReader
.It won't really improve performance or have any other beneficial effects, however. You should simply use
reader1
and not create the second one.It can even have negative effects if you start mixing calls to
reader1
andreader2
, but that's just the general idea of stream in Java: once you wrap a stream (or reader/writer), you should no longer access it directly.是的,您可以用
BufferedReader
装饰任何Reader
,但它不会给您带来任何好处。Yes, you can decorate any
Reader
with aBufferedReader
, but it doesn't give you any benefit.它将使用下面的构造函数来创建第二个 BufferedReader。我认为会有细微的性能损失,因为现在您有两个大小相同的缓冲区。
http://developer.classpath.org/doc/java/io/BufferedReader -source.html
It will use the below constructor to create the second BufferedReader. I would assume there would be a subtle performance loss because now you have two buffers with the same size.
http://developer.classpath.org/doc/java/io/BufferedReader-source.html