简体   繁体   English

java.io作家和读者 - 你怎么知道应该使用哪些?

[英]java.io writers and readers - how do you know which should be used?

There are quite a few classes that extend java.io.Writer and java.io.Reader. 有很多类扩展了java.io.Writer和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. 谢谢。

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. FileReader / StringReader / CharArrayReader / InputStreamReader具有不同的实现,可以从Files,Strings,CharArrays或InputStream中读取。 The usage depends on what your source is. 用法取决于您的来源。

LineNumberReader / PushbackReader / BufferedReader don't work 'Standalone' you can combine them with another source-Reader. LineNumberReader / PushbackReader / BufferedReader不能工作'Standalone'你可以将它们与另一个源-Reader结合使用。 for example new BufferedReader( new FileReader(file) ); 例如new BufferedReader( new FileReader(file) );

This Reader gives you methods for special cases you may want to do. 此Reader为您提供了可能需要执行的特殊情况的方法。 BufferedReader to read line by line, or LineNumberReader to get the line number. BufferedReader逐行读取,或LineNumberReader获取行号。

PipedReader are good to read data form on Part/Thread of your Program to another Part/Thread in combination with an PipedWriter . PipedReader很适合将程序的Part / Thread上的数据表格与PipedWriter一起读取到另一个Part / Thread。 ... ...

... for special cases you can build Chains with Reader and Writer like the Decorator pattern. ...对于特殊情况,您可以使用Reader和Writer构建链,就像Decorator模式一样。

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 . 如果你想从文件中读取,那么你可能需要一个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. 这是一个很好的问题我刚才问过,关于使用正确的Writer类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM