java源碼FileInputStream

來源:互聯網
上載者:User

近做一個很簡單的函數,要兩個傳回值,我打算有一個類封裝一下返回,後來領導要求使用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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.