java.io 作者和读者 - 您如何知道应该使用哪个?
有相当多的类扩展了 java.io.Writer 和 java.io.Reader。
在某些情况下您应该使用其中一种而不是另一种吗?它们各自的总体用途是什么(为什么有这么多)?
它们有不同的“性能”属性吗?它们都只是从流中写入/读取 - 不是吗?
有谁知道一篇文章可以给我一些例子来说明你会在哪里使用一个而不是另一个。
同样的问题也适用于处理实际文件。似乎有不止一种方法可以打开要读/写的文件流。
谢谢。
There are quite a few classes that extend java.io.Writer and java.io.Reader.
Are there circumstances where you should use one over another? What is the overall usefulness of each of them (why are there so many)?
Do they have difference "performance" attributes? They all just write/read from a stream - don't they?
Does anyone know of a write-up which will give me examples of where you would use one over another.
Same question applies to dealing with actual Files too. There seems to be more than one way to open a file stream to be read/written to.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
读者的名字说明了很多关于用例的信息。
FileReader / StringReader / CharArrayReader / InputStreamReader 有不同的实现来读取文件、字符串、CharArrays 或 InputStream。用法取决于您的来源。
LineNumberReader / PushbackReader / BufferedReader 不能“独立”工作,您可以将它们与另一个源阅读器结合使用。例如
new BufferedReader( new FileReader(file) );
此 Reader 为您提供了针对您可能想要执行的特殊情况的方法。
BufferedReader
逐行读取,或 LineNumberReader 获取行号。PipedReader
非常适合与PipedWriter
结合使用,将程序的部件/线程上的数据形式读取到另一个部件/线程。 ......对于特殊情况,您可以像装饰器模式一样使用 Reader 和 Writer 构建链。
The Name of the Reader says much about the use case.
FileReader / StringReader / CharArrayReader / InputStreamReader have different implementation to read from Files, Strings, CharArrays or InputStream. The usage depends on what your source is.
LineNumberReader / PushbackReader / BufferedReader don't work 'Standalone' you can combine them with another source-Reader. for example
new BufferedReader( new FileReader(file) );
This Reader gives you methods for special cases you may want to do.
BufferedReader
to read line by line, or LineNumberReader to get the line number.PipedReader
are good to read data form on Part/Thread of your Program to another Part/Thread in combination with anPipedWriter
. ...... for special cases you can build Chains with Reader and Writer like the Decorator pattern.
这是他们选择的特定设计模式。您需要哪个课程取决于“您需要做什么”。如果您想从文件中读取数据,那么您可能需要一个
FileReader
。对于大多数这些类,您可以将一个类包装到另一个类中以获得您需要的功能。这是我问的一个很好的问题不久前,关于使用正确的 Writer 类。
That's a particular design pattern that they have chosen. Which class you need is based what on "what you need do". If you want to read from a file you then perhaps need a
FileReader
. With most of those classes you can wrap one into another to get the functionality you need.Here's a nice question I've asked a while ago, about using right
Writer
class.