问题描述
除了初始化两者之外,我似乎无法确定 InputStreamReader
和 FileReader
之间的任何区别。使用其中一个有什么好处吗?大多数其他文章涵盖 FileInputStream
vs InputStreamReader
,但我与 FileReader $ c形成鲜明对比$ c>相反。在我看来,他们都有相同的目的。
I can't seem to determine any difference between InputStreamReader
and FileReader
besides the way the two are initialized. Is there any benefit to using one or the other? Most other articles cover FileInputStream
vs InputStreamReader
, but I am contrasting with FileReader
instead. Seems to me they both have the same purpose.
推荐答案
首先, InputStreamReader
可以处理所有输入流,而不仅仅是文件。其他示例是网络连接,类路径资源和ZIP文件。
First, InputStreamReader
can handle all input streams, not just files. Other examples are network connections, classpath resources and ZIP files.
其次, FileReader
不允许您指定编码,而是使用平台默认编码,这使得它几乎无用,因为当代码在具有不同平台默认编码的系统上运行时,使用它将导致数据损坏。
Second, FileReader
does not allow you to specify an encoding and instead uses the plaform default encoding, which makes it pretty much useless as using it will result in corrupted data when the code is run on systems with different platform default encodings.
简而言之,忘记 FileReader
存在。
In short, forget that FileReader
exists.
这篇关于InputStreamReader与FileReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!