天天看點

java .io OutputStream 與InputStream

 outputStream首先聲明這是一個抽象類,是以關于輸出的類都繼承與這個類。

三個基本的寫方法

abstract void write(int b) :往輸出流中寫入指定的位元組。

void write(byte[] b) :往輸出流中寫入數組b中的所有位元組。

void write(byte[] b, int off, int len) :往輸出流中寫入數組b中從偏移量off開始的len個位元組的資料

記住write的操作都是對byte 資料操作。

Output streams exist to allow data to be written to some data consumer; what sort ofconsumer is unimportant because the output stream objects define methods that allow data to be sent to any sort of data consumer。

注釋:Even though the write methods specify that they accept ints, they are actually accepting bytes. Only the lower 8 bytes of the int are actually used.

byte b = new byte[100]; // creates a byte array

output.write( b ); // writes the byte array

以上的output為OutputStream類型,寫入資料到b中。

If an output stream is an abstract class, where does it come from? How do you instantiate an OutputStream class? OutputStream objects are never obtained directly by using the new operator. Rather, OutputStream objects are usually obtained from other objects. For example, the Socket class contains a method called getOutputStream. Calling the  getOutputStream method will return an OutputStream object that will be used to write to the socket. Other output streams are obtained by different means.

關閉流public void close()

OutputStream對象一般由其他類的方法來得到。

read.txt如果不存在,會建立一個新的。

InputStream

The InputStream class provided by Java is abstract, and it is only meant to be overridden to provide InputStream classes for such things as socket- and disk-based input. The InputStream provided by Java provides the following methods:

public abstract int read()  Reads the next byte of data from the input stream. The value byte is returned as an <code>int</code> in the range <code>0</code> to <code>255</code>. If no byte is available because the end of the stream has been reached, the value <code>-1</code> is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

public int read(byte[] b)

public int read(byte[] b, int off, int len)

 public void close()

Any I/O operation can be accomplished with the InputStream and OutputStream classes. These classes are like atoms: you can build anything with them, but they are very basic building blocks. The InputStream and OutputStream classes only give you access to the raw bytes of the connection. It’s up to you to determine whether the underlying meaning of these bytes is a string, an IEEE754 floating point number, Unicode text, or some other binary construct.

Filters are generally used as a sort of attachment to the InputStream and OutputStream classes to hide the low-level complexity of working solely with bytes. There are two primary types of filters. The first is the basic filter, which is used to transform the underlying binary numbers into meaningful data types. Many different basic filters have

been created; there are filters to compress, encrypt, and perform various translations on data.

 Read Filter                  Write Filter                             

BufferedInputStream  BufferedOutputStream

DataInputStream   DataOutputStream

GZIPInputStream  GZIPOutputStream

等等的過濾器。

The second type of filter is really a set of filters that work together; the filters that compose  this set are called readers and writers.

Filters themselves are extended from the FilterInputStream and  FilterOutputStream classes。即4這些filter都是繼承了FilterInputS和FilterOutputStream類。而這兩個類又繼承了java.io.InputStream 和OutputStream這2個類。正因為如此,有read和write方法。

Chaining Filters Together

One very important feature of filters is their ability to chain themselves together. A basic filter can be layered on top of either an input/output stream or another filter. A reader/writer can be layered on top of an input/output stream or another filter but never on another reader/ writer.Readers and writers must always be the last filter in a chain.

reader write 總是鍊條的最後一個。

Filters are layered by passing the underlying filter or stream into the constructor of the new

stream. For example, to open a file with a BufferedInputStream, the following code should be used:

FileInputStream fin = new FileInputStream("myfile.txt");

BufferedInputStream bis = new BufferedInputStream(fin);

 其實BufferedInputStream的參數類型為InputStream ,我們這裡的是FileInputStream類型,由于基類是InputStream,是以這裡有一個隐士轉換。

    InputStream不可以讀取檔案,它是一個Abstract的類,根本不可能執行個體化,是所有輸入流的基類。而FileInputStream是InputStream的一個實作類,用于讀取諸如圖像資料之類的原始位元組流。要讀取字元流,請考慮使用 FileReader 。

java.lang.Object

        java.io.InputStream

              java.io.FileInputStream