FilterInputStream類要完成兩件全然不同的事情。其中,DataInputStream允許我們讀取不同的基本類型數據以及String對象(所有方法都以“read”開頭,比如readByte(),readFloat()等等)。伴隨對應的DataOutputStream,我們可通過數據“流”將基本類型的數據從一個地方搬到另一個地方。這些“地方”是由表10.1總結的那些類決定的。若讀取塊內的數據,並自己進行解析,就不需要用到DataInputStream。但在其他許多情況下,我們一般都想用它對自己讀入的數據進行自動格式化。
剩下的類用於修改InputStream的內部行為方式:是否進行緩沖,是否跟蹤自己讀入的數據行,以及是否能夠推回一個字符等等。後兩種類看起來特別象提供對構建一個編譯器的支持(換言之,添加它們為了支持Java編譯器的構建),所以在常規編程中一般都用不著它們。
也許幾乎每次都要緩沖自己的輸入,無論連接的是哪個IO設備。所以IO庫最明智的做法就是將未緩沖輸入作為一種特殊情況處理,同時將緩沖輸入接納為標准做法。
表10.3 FilterInputStream的類型
Class
Function
Constructor Arguments
How to use it
Data-InputStream
Used in concert with DataOutputStream, so you can read primitives (int, char, long, etc.) from a stream in a portable fashion.
InputStream
Contains a full interface to allow you to read primitive types.
Buffered-InputStream
Use this to prevent a physical read every time you want more data. You’re saying “Use a buffer.”
InputStream, with optional buffer size.
This doesn’t provide an interface per se, just a requirement that a buffer be used. Attach an interface object.
LineNumber-InputStream
Keeps track of line numbers in the input stream; you can call getLineNumber() and setLineNumber(int).
InputStream
This just adds line numbering, so you’ll probably attach an interface object.
Pushback-InputStream
Has a one byte push-back buffer so that you can push back the last character read.
InputStream
Generally used in the scanner for a compiler and probably included because the Java compiler needed it. You probably won’t use this.