构造 BufferedReader,以 BufferedReader 作为读取器

发布于 2024-12-08 10:29:03 字数 240 浏览 0 评论 0原文

我可以做这个科目吗?我的意思是:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗫嚅 2024-12-15 10:29:03

从它可以工作的意义上来说,它是正确的:

您可以从任何有效的 Reader,甚至另一个 BufferedReader 构造一个 BufferedReader。

然而,它不会真正提高性能或产生任何其他有益效果。您应该只使用 reader1 而不是创建第二个。

如果您开始混合调用reader1reader2,它甚至可能会产生负面影响,但这只是Java中流的一般概念:一旦您包装流(或读取器/写入器),您不应再直接访问它。

It is correct in the sense that it will work:

You can construct a BufferedReader from any valid Reader, even another BufferedReader.

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 and reader2, 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.

不回头走下去 2024-12-15 10:29:03

是的,您可以用 BufferedReader 装饰任何 Reader,但它不会给您带来任何好处。

Yes, you can decorate any Reader with a BufferedReader, but it doesn't give you any benefit.

一梦等七年七年为一梦 2024-12-15 10:29:03

它将使用下面的构造函数来创建第二个 BufferedReader。我认为会有细微的性能损失,因为现在您有两个大小相同的缓冲区。

 /**
  * Creates a buffering character-input stream that uses a default-sized
  * input buffer.
  *
  * @param  in   A Reader
  */
  public BufferedReader(Reader in) {
      this (in, defaultCharBufferSize);
  }

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.

 /**
  * Creates a buffering character-input stream that uses a default-sized
  * input buffer.
  *
  * @param  in   A Reader
  */
  public BufferedReader(Reader in) {
      this (in, defaultCharBufferSize);
  }

http://developer.classpath.org/doc/java/io/BufferedReader-source.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文