2013-05-10 37 views

回答

1

FileChannel有許多功能的java.io缺少的:它是中斷的,它可以在文件中移動位置時,它可以鎖定文件等。而且它可以比舊IO速度更快,特別是當它使用直接字節的緩衝區這裏是從ByteBuffer的API的解釋:

byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations. 

如果你需要沒有上述特徵去流,你會得到一個更短的代碼。

+0

底線:除了直接內存訪問,並且不考慮中斷的事情是一樣的嗎? – Rollerball 2013-05-10 09:07:29

+0

是的,如果你只想讀取文件內容,加快並不重要,結果將是相同的。 FileChannel有2個缺點 - peoople很少使用它,它可以與字節只有工作,而每個程序員都知道流和數據流可以用於閱讀的文本。 – 2013-05-10 09:12:29

+0

關於字節的東西。它仍然可以通過Charset類在CharBuffer中進行轉換。我同意 – Rollerball 2013-05-10 10:38:02