與DataInputStream對應的是DataOutputStream,後者對各個基本數據類型以及String對象進行格式化,並將其置入一個數據“流”中,以便任何機器上的DataInputStream都能正常地讀取它們。所有方法都以“wirte”開頭,例如writeByte(),writeFloat()等等。
若想進行一些真正的格式化輸出,比如輸出到控制台,請使用PrintStream。利用它可以打印出所有基本數據類型以及String對象,並可采用一種易於查看的格式。這與DataOutputStream正好相反,後者的目標是將那些數據置入一個數據流中,以便DataInputStream能夠方便地重新構造它們。System.out靜態對象是一個PrintStream。
PrintStream內兩個重要的方法是print()和println()。它們已進行了覆蓋處理,可打印出所有數據類型。print()和println()之間的差異是後者在操作完畢後會自動添加一個新行。
BufferedOutputStream屬於一種“修改器”,用於指示數據流使用緩沖技術,使自己不必每次都向流內物理性地寫入數據。通常都應將它應用於文件處理和控制器IO。
表10.4 FilterOutputStream的類型
Class
Function
Constructor Arguments
How to use it
Data-OutputStream
Used in concert with DataInputStream so you can write primitives (int, char, long, etc.) to a stream in a portable fashion.
OutputStream
Contains full interface to allow you to write primitive types.
PrintStream
For producing formatted output. While DataOutputStream handles the storage of data, PrintStream handles display.
OutputStream, with optional boolean indicating that the buffer is flushed with every newline.
Should be the “final” wrapping for your OutputStream object. You’ll probably use this a lot.
Buffered-OutputStream
Use this to prevent a physical write every time you send a piece of data. You’re saying “Use a buffer.” You can call flush() to flush the buffer.
OutputStream, with optional buffer size.
This doesn’t provide an interface per se, just a requirement that a buffer is used. Attach an interface object.