Java I/O 流;有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1160050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java I/O streams; what are the differences?
提问by Lawrence
java.iohas many different I/Ostreams, (FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedStreams... etc.) and I am confused in determining the differences between them. What are some examples where one stream type is preferred over another, and what are the real differences between them?
java.io有许多不同的I/O流,(FileInputStream、FileOutputStream、FileReader、FileWriter、BufferedStreams...等),我在确定它们之间的差异时感到困惑。有哪些流类型优于另一种流类型的示例,它们之间的真正区别是什么?
采纳答案by Andrew Hare
This is a big topic! I would recommend that you begin by reading I/O Streams:
这是一个很大的话题!我建议您从阅读I/O Streams 开始:
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.
I/O Stream 表示输入源或输出目的地。流可以表示许多不同类型的源和目标,包括磁盘文件、设备、其他程序和内存阵列。
流支持许多不同类型的数据,包括简单字节、原始数据类型、本地化字符和对象。有些流只是简单地传递数据;其他人以有用的方式操作和转换数据。
回答by Michael Myers
Streams:one byte at a time. Good for binary data.
流:一次一个字节。适用于二进制数据。
Readers/Writers:one character at a time. Good for text data.
读者/作家:一次一个角色。适用于文本数据。
Anything "Buffered":many bytes/characters at a time. Good almost all the time.
任何“缓冲”的东西:一次很多字节/字符。几乎一直都很好。
回答by dfa
When learning Java I made this mental scheme about java.io:
在学习 Java 时,我对 java.io 做了一个心理计划:
Streams
流
- byte oriented stream (8 bit)
- good for binary data such as a Java .class file
- good for "machine-oriented" data
- 面向字节的流(8 位)
- 适用于二进制数据,例如 Java .class 文件
- 适用于“面向机器”的数据
Readers/Writers
读者/作家
- char (utf-16) oriented stream (16 bit)
- good for text such as a Java source
- good for "human-oriented" data
- 面向字符 (utf-16) 的流(16 位)
- 适用于 Java 源代码等文本
- 适合“以人为本”的数据
Buffered
缓冲的
- always useful unless proven otherwise
- 除非另有证明,否则总是有用的
回答by T0xicCode
Separate each name into words: each capital is a different word.
将每个名字分成单词:每个大写是一个不同的单词。
- File Input Streamis to get Inputfrom a Fileusing a Stream.
- File Output Streamis to write Outputto a Fileusing a Stream
- 文件输入流是使用Stream从文件中获取输入。
- 文件输出流是写输出到文件使用流
And so on and so forth
等等等等
As mmyers wrote :
正如 mmyers 写道:
Streams: one byte at a time.
Readers/Writers: one character at a time.
Buffered*: many bytes/characters at a time.
流:一次一个字节。
读者/作家:一次一个角色。
缓冲*:一次处理多个字节/字符。
回答by Shervin Asgari
I also found this java_tip_how_read_files_quickly
我也发现了这个java_tip_how_read_files_quickly
Very useful! It shows which streams are most efficient.
很有用!它显示了哪些流最有效。
回答by Jakob Jenkov
This is probably the most thorough overview of the various streams, Reader's and Writer's in the Java IO API:
这可能是对 Java IO API 中各种流、Reader 和 Writer 的最全面的概述:
http://tutorials.jenkov.com/java-io/overview.html
http://tutorials.jenkov.com/java-io/overview.html
It's part of a larger Java IO tutorial covering both byte and charater based streams.
它是更大的 Java IO 教程的一部分,涵盖了基于字节和字符的流。
It also covers streams that are used for reading and writing raw numeric data, like int's float's etc.
它还涵盖了用于读取和写入原始数字数据的流,例如 int 的 float 等。
It also covers streams used for parsing like the PushbackInputStream and the PushbackReader.
它还涵盖用于解析的流,如 PushbackInputStream 和 PushbackReader。
回答by PaulJWilliams
The specialisations you mention are specific types used to provide a standard interface to a variety of data sources. For example, a FileInputStream and an ObjectInputStream will both implement the InputStream interface, but will operate on Files and Objects respectively.
您提到的专业化是用于为各种数据源提供标准接口的特定类型。例如,FileInputStream 和 ObjectInputStream 都将实现 InputStream 接口,但将分别对 Files 和 Objects 进行操作。
回答by i2ijeya
Byte streams are mostly and widely used stream type in java 1.0 for both character and for byte. After java 1.0 it was deprecated and character streams plays a important role. ie., for example
字节流是 java 1.0 中主要和广泛使用的流类型,用于字符和字节。在 java 1.0 之后它被弃用,字符流起着重要的作用。即,例如
BufferedReader will get the character from the source, and its constructor looks like BufferedReader(Reader inputReader)..
BufferedReader 将从源中获取字符,其构造函数看起来像 BufferedReader(Reader inputReader)..
Here Reader is an abstract class and the once of its concrete classes are InputStreamReader, which will converts bytes into characters and take input from the keyboard(System.in)...
这里 Reader 是一个抽象类,它的一个具体类是 InputStreamReader,它将字节转换为字符并从键盘(System.in)获取输入...
BufferedReader : Contains internal Buffer that will read characters from the stream. Internal counter keeps track of next character to be supplied to the buffer thru read(). InputStreamReader will takes input as bytes and converts internally into characters.
BufferedReader :包含将从流中读取字符的内部缓冲区。内部计数器通过 read() 跟踪要提供给缓冲区的下一个字符。InputStreamReader 将输入作为字节并在内部转换为字符。
回答by Toumi
Java input and output is defined in terms of an abstract concept called a “stream”, which is a sequence of data. There are 2 kinds of streams.
Java 输入和输出是根据一个称为“流”的抽象概念定义的,它是一个数据序列。有2种流。
- Byte streams (8 bit bytes) ? Abstract classes are: InputStream and OutputStream
- Character streams (16 bit UNICODE) ? Abstract classes are: Reader and Writer
- 字节流(8 位字节) ? 抽象类是: InputStream 和 OutputStream
- 字符流(16 位 UNICODE) ? 抽象类是:Reader 和 Writer
java.io.* classes use the decorator design pattern. The decorator design pattern attaches
responsibilities to objects at runtime. Decorators are more flexible than inheritance because the inheritance
attaches responsibility to classes at compile time. The java.io.* classes use the decorator pattern to construct
different combinations of behavior at runtime based on some basic classes.


java.io.* 类使用装饰器设计模式。装饰器设计模式在运行时将责任附加到对象上。装饰器比继承更灵活,因为继承在编译时将责任附加到类。java.io.* 类使用装饰器模式基于一些基本类在运行时构造不同的行为组合。


from the book Java/J2EE Job Interview Companion By K.Arulkumaran & A.Sivayini
来自K.Arulkumaran 和 A.Sivayini 的 Java/J2EE Job Interview Companion 这本书

