Java 版 (精华区)

发信人: DreamWeaver (猪头小队长·无雨无风), 信区: Java
标  题: 来自 I/O 的困惑
发信站: 哈工大紫丁香 (2002年10月25日09:37:05 星期五), 站内信件

发信人: javarookie (JavaRookie), 信区: Java
标  题: 来自 I/O 的困惑
发信站: BBS 水木清华站 (Thu Oct 24 21:33:00 2002), 转信

来自 IO 的困惑

从一开始学习 Java 我就一直对 IO 感到很困惑:
Java 里面为什么有这么多有关 IO 操作的类?
到底用哪一个好呢?
哪一个效率更高呢?
要读写中文的话怎么办呢?

真是揪心的问题啊
即使写完了这篇文章我也仍然感到 Java 的 IO 是个永恒的话题
其实我这篇文章里面的内容基本上都摘自 SUN 的 SCJP SL-275教材
它详细明确的列出了各个 IO 类,并进行分类描述
有各个IO类的功能描述,真的是觉得不错,拿出来与大家共享之
内容大都是表格和图表,很简洁明了

Stream最基本的分为:
1. source stream 其实就是 input stream
2. sink stream 其实就是 output stream
因此,stream 就是一个由 source 到 sink 的数据流
Sources 和 Sinks 都属于 Node Streams


一、基础 Stream 的类包括两大类:
+-----------------+---------------+-------------------+
|                 | byte streams  | character streams |
|-----------------+---------------+-------------------|
| source streams  | InputStream   |      Reader       |
|-----------------+---------------+-------------------|
| sink streams    | OutputStream  |      Writer       |
+-----------------+---------------+-------------------+
byte streams类支持操作的数据为raw bytes
character streams类支持操作的数据为Unicode characters
因此涉及到中文或其他 Unicode 字符操作时都要使用character streams

习惯上将 stream 特指 byte streams,将reader/writer特指character streams
(当看到类名中包含InputStream/OutputStream就是操作byte stream的,
包含Reader/Writer就是操作character stream的)


二、Node Streams节点流类包括三种基本类:
files, memory, pipes
+-------------+--------------------+-----------------------+
|     Type    | Character Streams  | Byte Streams          |
|-------------+--------------------+-----------------------|
|     File    | FileReader         | FileInputStream       |
|             | FileWriter         | FileOutputStream      |
|-------------+--------------------+-----------------------|
|Memory:Array | CharArrayReader    | ByteArrayInputStream  |
|             | CharArrayWriter    | ByteArrayOutputStream |
|-------------+--------------------+-----------------------|
|Memory:String| StringReader       |                       |
|             | StringWriter       |                       |
|-------------+--------------------+-----------------------|
|     Pipe    | PipedReader        | PipedInputStream      |
|             | PipedWriter        | PipedOutputStream     |
+-------------+--------------------+-----------------------+

三、I/O Stream Chaining IO链流:
1. Input Stream Chain
                                          +----------------+
                   +----------------------|                |
+------------------|                      |                |
|FileInputStream-->|BufferedInputStream-->|DataInputStream |
+------------------|                      |                |
       A           +----------------------|                |
       |                                  +----------------+
       |                                           |
       |                                           V
  +----------+                                 +-------+
  |DataSource|                                 |Program|
  +----------+                                 +-------+

2. Output Stream Chain
+-------------------+
|                   |-----------------------+
|                   |                       |----------------+
|DataOutputStream-->|BufferedOutputStream-->|FileOutputStream|
|                   |                       |----------------+
|                   |-----------------------+        |
+-------------------+                                |
         A                                           |
         |                                           V
     +-------+                                   +--------+
     |Program|                                   |DataSink|
     +-------+                                   +--------+


四、Processing Streams 进程流:
Processing Streams 也叫 filter streams 筛选流(过滤流)
Processing Streams 可以提供Byte Streams 和 Character Streams互相转换
+--------------------+--------------------+-----------------------+
| Type               | Character Streams  | Byte Streams          |
|--------------------+--------------------+-----------------------|
| Buffering          | BufferedReader     | BufferedInputStream   |
|                    | BufferedWriter     | BufferedOutputStream  |
|--------------------+--------------------+-----------------------|
| Filtering          | FilterReader       | FilterInputStream     |
| (abstract class)   | FilterWriter       | FilterOutputStream    |
|--------------------+--------------------+-----------------------|
| Converting between | InputStreamReader  |                       |
| bytes and character| OuptutStreamWriter |                       |
|--------------------+--------------------+-----------------------|
| Object             |                    | ObjectInputStream     |
| serialization      |                    | ObjectOutputStream    |
|--------------------+--------------------+-----------------------|
| Data conversion    |                    | DataInputStream       |
|                    |                    | DataOutputStream      |
|--------------------+--------------------+-----------------------|
| Counting           | LineNumberReader   | LineNumberInputStream |
|--------------------+--------------------+-----------------------|
| Peeking ahead      | PushbackReader     | PushbackInputStream   |
|--------------------+--------------------+-----------------------|
| Printing           | PrintWriter        | PrintStream           |
+--------------------+--------------------+-----------------------+


五、IO 类层次结构
1. 基本的 Byte Stream 类层次结构:

1) InputStream类层次结构:

                 +--- FileInputStream
                 |
                 +--- ObjectInputStream      +--- DataInputStream
                 |                           |
+-----------+    |    +-----------------+    +--- PushbackInputStream
|InputStream| <--+--- |FilterInputStream| <--|
+-----------+    |    +-----------------+    +--- BufferedInputStream
                 |                           |
                 +--- PipedInputStream       +--- LineNumberInputStream
                 |
                 +--- SequenceInputStream
                 |
                 +--- StringBufferInputStream
                 |
                 +--- ByteArrayInputStream

2) OutputStream类层次结构:

                  +--- FileOutputStream
                  |
                  +--- ObjectOutputStream
                  |                            +--- DataOutputStream
+------------+    |    +------------------+    |
|OutputStream| <--+--- |FilterOutputStream| <------ BufferedOutputStream
+------------+    |    +------------------+    |
                  |                            +--- PrintStream
                  +--- PipedOutputStream
                  |
                  +--- ByteArrayOutputStream

2. 基本的 Character Stream 类层次结构:

1) Reader类层次结构:

            +--- BufferedReader <---- LineNumberReader
            |
            +--- CharArrayReader
            |
+------+    +--- StringReader
|Reader| <--|
+------+    +--- InputStreamReader <--- FileReader
            |
            +--- PipedReader
            |
            +--- FilterReader <--- PushbackReader

2) Writer类层次结构:

            +--- BufferedWriter <--- LineNumberReader
            |
            +--- CharArrayWriter
            |
            +--- StringWriter
+------+    |
|Writer| <--+--- OutputStreamWriter <--- FileWriter
+------+    |
            +--- PrintWriter
            |
            +--- PipedWriter
            |
            +--- FilterWriter


小结:
SL-275推荐在程序中尽量使用最底层的子类以提高效率
(不过我觉得从各种source输入的流应该需要IO链流一步一步转化为
最底层的高效率的IO子类,下面的例子就是如此)


六、一个字符在对象之间流动的过程示意:

1. 对应的程序:
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
String name = input.readLine();
System.out.println(name);

2. 流程示意图:
设备(键盘)
    |     System.in
    +---> |65|108|32|71|111|114|101|13|-----+
          InputStream 对象                  |
                                            |
          reader                            |
    +---- |65|108|32|71|111|114|101|13| <---+
    |     InputStreamReader对象
    |
    |     input
    +---> |A|l| |G|o|r|e|\n| ---+
          BufferedReader对象    |
                                |
           name                 |
    +---- "Al Gore" <-----------+
    |     String对象
    |
    |      System.out
    |
    |      System.out
    +---> |H|e|l|l|o|,|A|l| |G|o|r|e|!| | ---+
          PrintStream对象                    |
                                             |
                       +---------------------+
                       |
                       V
           显示器输出:Hello,Al Gore!


--

         忘掉她,忘掉她就可以不必再忍受,忘掉她就不再有痛苦。忘掉她,忘掉
     你没有的东西,忘掉别人有的东西,忘掉你失去和以后不能得到的东西,忘掉
     仇恨,忘掉屈辱,忘掉爱情,象犀牛忘掉草原,象水鸟忘掉湖泊,象地狱里的
     人忘掉天堂,象截肢的人忘掉自己曾快步如飞,象落叶忘掉风,象公犀牛忘掉
     母犀牛。忘掉是一般人能做的唯一的事。但是我决定不忘掉她。                    

※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: dw.hit.edu.cn]
※ 修改:·DreamWeaver 於 10月25日09:37:34 修改本文·[FROM: dw.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:203.480毫秒