近做一個很簡單的函數,要兩個傳回值,我打算有一個類封裝一下返回,後來領導要求使用StringBuilder返回,不用StringBuffer,因為局部變數StringBuffer最後還是要編譯為StringBuilder,因為StringBuilder沒有任何同步。
問了一下大家的看法,風南說InputStream 的int read(byte[] b) 從輸入資料流中讀取一定數量的位元組並將其儲存在緩衝區數組 b 中,也是類似的,看來有必要研究一下IO了。
InputStream類中重要的函數,只有一個抽象的有用,
public abstract int read() throws IOException;
其他兩個read的意義不大。
public int read(byte b[], int off, int len) throws IOException 和
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
下面看它的子類FileInputStream的實現,
/* File Descriptor - handle to the open file */
private FileDescriptor fd;
這個很重要,這是檔案控制代碼,C語言使用fopen函數的都知道。
/**
* Opens the specified file for reading.
* @param name the name of the file
*/
private native void open(String name) throws FileNotFoundException;
/**
* Reads a byte of data from this input stream. This method blocks
* if no input is yet available.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* file is reached.
* @exception IOException if an I/O error occurs.
*/
public native int read() throws IOException;
/**
* Reads a subarray as a sequence of bytes.
* @param b the data to be written
* @param off the start offset in the data
* @param len the number of bytes that are written
* @exception IOException If an I/O error has occurred.
*/
private native int readBytes(byte b[], int off, int len) throws IOException;