知識點一:分類
IO中按照數據流的方向不同可以分為輸入流和輸出流(以程序的角度來考慮)
按照數據單位的不同可以分為字節流和字符流。
按照功能的不同可以分為節點流和處理流。
知識點二: 四大等級結構
java語言的i/o庫提供了四大等級結構:InputStream,OutputStream,Reader,Writer四個系列的類。InputStream和OutputStream處理8位字節流數據, Reader和Writer處理16位的字符流數據。InputStream和Reader處理輸入, OutputStream和Writer處理輸出。大家一定要到J2SE文檔中看看這四大等級結構的類繼承體系。
除了這四大系列類,i/o庫還提供了少數的輔助類,其中比較重要的是InputStreamReader和OutputStreamWriter。InputStreamReader把InputStream適配為Reader, OutputStreamWriter把OutputStream適配為Writer;這樣就架起了字節流處理類和字符流處理類間的橋梁。
您使用I/O庫時,只要按以上的規則,到相應的類體系中尋找您需要的類即可
下面我就會對I/O 進行不定期更新:
1 FileOutputStream 文件字節流
Public class FileInputStream extends InputStream
{
/* File Descriptor - handle to the open file */
private FileDescriptor fd;
public FileInputStream(FileDescriptor fdObj)
{
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
}
//其他代碼
}
可見,FileInputStream繼承了InputStream,組合了FileDescriptor,采用的是對象Adapter模式。我們學習i/o庫時,主要應該掌握這四個對象Adapter模式的適配源: ByteArrayInputStream的適配源是Byte數組, FileInputStream的適配源是File對象, PipedInputStream的適配源是PipedOutputStream對象, StringBufferInputStream的適配源是String對象