第11篇-JAVA IO
每篇一句 :不要在失敗時纔想起別人的忠告,不要在失去之後纔想起珍惜
初學心得: 環境越艱難,越會出聰明人
(筆者:JEEP/711)[JAVA筆記 | 時間:2017-04-17| JAVA IO流 ]
1.流概念
流是指一連串流動的字符,是以先進先出方式發送信息的通道
即數據在兩設備間的傳輸稱爲流,流的本質是數據傳輸,根據數據傳輸特性將流抽象爲各種類,方便更直觀的進行數據操作
2.JAVA IO流
Java的IO流是實現輸入/輸出的基礎,它可以方便地實現數據的輸入/輸出操作
Java中把不同的輸入/輸出源(鍵盤、文件、網絡連接等)抽象表述爲“流”(stream),通過流的方式允許Java程序使用相同的方式來訪問不同的輸入/輸出源。stream是從起源(source)到接收(sink)的有序數據
Java把所有傳統的個流類型(類或抽象類)都放在java.io包中,用以實現輸入/輸出功能
3.JAVA IO流分類
按流向來分類:可以分爲輸入流和輸出流:
輸入流:只能從中讀取數據,而不能向其寫出數據
輸出流:只能向其寫出數據,而不能從中讀取數據
字節流和字符流:
按照流的角色分,可以分爲節點流和處理流IO流的四個基類:
Java把所有設備裏的有序數據抽象成流模型簡化了輸入/輸出的處理
Java的IO流共涉及到40多個類,這些類看上去蕪雜而凌亂,但實際上是非常規則、而且彼此之間存在非常緊密的聯繫
Java的IO流的40多個類都是從4個抽象基類派生出來的:
InputStream/Reader:所有輸入流的基類,前者是輸入字節流,後者是輸入字符。
OutputStream/Writer:所有輸出流的基類,前者是輸出字節流,後者是輸出字符流
- 輸入流:InputStream和Reader是所有輸入流的基類,它們都是兩個抽象類,本身並不能創建實例來執行輸入,但它們將所謂所有輸入流的模板,所以它們的方法是所有輸入流都可使用的方法
int read():從輸入流中讀取單個字節(相當於從圖15.5所示水管中取出一滴水),返回所讀取的字節數據(字節數據可直接轉換爲int類型)
int read(byte[]/char[] b):從輸入流中讀取最多b.length個字節的數據,並將其存儲在字節數組b中,返回實際讀取的字節數
int read(byte[]/char[] b, int off, int len):從輸入流中讀取最多len字節的數據,並將其存儲在數組 b 中,放入b數組中時,並不是從數組起點開始,而是從off位置開始,返回實際讀取的字節數
- 輸出流:OutputStream和Writer也非常相似,它們採用模型來執行輸出,兩個流都提供瞭如下三個方法
void write(int c):將指定的字節/字符輸出到輸出流中,其中c既可以代表字節,也可以代表字符
void write(byte[]/char[] buf):將字節數組/字符數組中的數據輸出到指定輸出流中
void write(char[] cbuf, int off, int len):將字節數組/字符數組中從off位置開始,長度爲len的字節/字符輸出到輸出流中
處理流的用法:
使用處理流來包裝節點流,程序通過處理流來執行輸入/輸出功能,讓節點流與底層的I/O設備、文件交互
程序使用處理流非常簡單,通常只需要在創建處理流時傳入一個節點流作爲構造器參數即可,這樣創建的處理流就是包裝了該節點流的處理流
4.流的操作規律
1、明確源和目的。
數據源:就是需要讀取,可以使用兩個體系:InputStream、Reader;
數據匯:就是需要寫入,可以使用兩個體系:OutputStream、Writer;2、操作的數據是否是純文本數據?
如果是:數據源:Reader
數據匯:Writer
如果不是:數據源:InputStream
數據匯:OutputStream3、雖然確定了一個體系,但是該體系中有太多的對象,到底用哪個呢?
明確操作的數據設備。
數據源對應的設備:硬盤(File),內存(數組),鍵盤(System.in)
數據匯對應的設備:硬盤(File),內存(數組),控制檯(System.out)4、需要在基本操作上附加其他功能嗎?比如緩衝,如果需要就進行裝飾
5.java.io包
文本文件的讀寫:
用FileInputStream讀文件
用FileOutputStream寫文件
用BufferedReader讀文本文件
用BufferedWriter寫文本文件
二進制文件的讀寫:
DataOutputStream
DataInputStream
6.字節流文件拷貝
1.public class IoTest { 2. public static void main(String[] args) throws IOException,InterruptedException { 3. String path = "c:\\b.txt"; 4. readFile(path); 5. } 6. private static void readFile(String path) { 7. FileInputStream fis = null; 8. try { 9. fis = new FileInputStream(path); 10. byte[] byt = new byte[1024]; 11. int len = fis.read(byt); 12. System.out.println(new String(byt, 0, len)); 13. } catch (IOException e) { 14. // 拋出運行時異常 15. throw new RuntimeException(e); 16. } finally { 17. // 把close方法放入finally中保證一定會執行 18. // 先判斷是否空指針 19. if (fis != null) { 20. try { 21. fis.close(); 22. } catch (Exception e) { 23. throw new RuntimeException(e); 24. } 25. } 26. } 27. } 28.}
1.public static void copyFile(String srcPath, String destPath) { 2. FileInputStream fis = null; 3. FileOutputStream fos = null; 4. try { 5. fis = new FileInputStream(srcPath); 6. fos = new FileOutputStream(destPath); 7. byte[] byt = new byte[1024 * 1024]; 8. int len = 0; 9. while ((len = fis.read(byt)) != -1) { 10. fos.write(byt, 0, len); 11. } 12. } catch (IOException e) { 13. throw new RuntimeException(e); 14. } finally { 15. try { 16. if (fis != null) { 17. fis.close(); 18. } 19. } catch (IOException e) { 20. throw new RuntimeException(e); 21. } finally { 22. if (fos != null) { 23. try { 24. fos.close(); 25. } catch (IOException e) { 26. throw new RuntimeException(e); 27. } 28. } 29. } 30. } 31. }
7.字符流文件拷貝
1.public static void main(String[] args) throws Exception { 2. String path1 = "c:/a.txt"; 3. String path2 = "c:/b.txt"; 4. copyFile2(path1, path2); 5. } 6./** 7. * 使用字符流拷貝文件,有完善的異常處理 8. */ 9.public static void copyFile2(String path1, String path2) { 10. Reader reader = null; 11. Writer writer = null; 12. try { 13. // 打開流 14. reader = new FileReader(path1); 15. writer = new FileWriter(path2); 16. // 進行拷貝 17. int ch = -1; 18. while ((ch = reader.read()) != -1) { 19. writer.write(ch); 20. } 21. } catch (Exception e) { 22. throw new RuntimeException(e); 23. } finally { 24. // 關閉流,注意一定要能執行到close()方法,所以都要放到finally代碼塊中 25. try { 26. if (reader != null) { 27. reader.close(); 28. } 29. } catch (Exception e) { 30. throw new RuntimeException(e); 31. } finally { 32. try { 33. if (writer != null) { 34. writer.close(); 35. } 36. } catch (Exception e) { 37. throw new RuntimeException(e); 38. } 39. } 40. } 41. }
初學(JAVA IO流 高級階段) 難點: ★★★★★
希望每一篇文章都能夠對讀者們提供幫助與提升,這乃是每一位筆者的初衷
感謝您的閱讀 歡迎您的留言與建議
FaceBook:JEEP SevenEleven
Twitter:@JEEP7ll
新浪官方微博: @JEEP-711
Blog Garden:http://www.cnblogs.com/JEEP711/
W3C/Blog:http://www.w3cschool.cn/jeep711blog/
CSDN/Blog:http://blog.csdn.net/jeep911
51CTO/Blog:http://jeep711.blog.51cto.com/
郵箱: [email protected],[email protected]