標籤:java io
第11篇-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是所有輸入資料流的基類,它們都是兩個抽象類別,本身並不能建立執行個體來執行輸入,但它們將所謂所有輸入資料流的模板,所以它們的方法是所有輸入資料流都可使用的方法
-
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的位元組/字元輸出到輸出資料流中
處理流的用法:
4.流的操作規律
-
1、明確源和目的。
資料來源:就是需要讀取,可以使用兩個體系:InputStream、Reader;
資料匯:就是需要寫入,可以使用兩個體系:OutputStream、Writer;
-
2、操作的資料是否是純文字資料?
如果是:資料來源:Reader
資料匯:Writer
如果不是:資料來源:InputStream
資料匯:OutputStream
-
3、雖然確定了一個體系,但是該體系中有太多的對象,到底用哪個呢?
明確操作的資料裝置。
資料來源對應的裝置:硬碟(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. // 先判斷是否null 指標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
-
Github部落格: https://github.com/jeep711/jeep711.github.io
-
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/
-
碼云:http://git.oschina.net/JEEP711/jeep711.github.io
-
郵箱: [email protected],[email protected]
本文出自 “JEEP711” 部落格,請務必保留此出處http://jeep711.blog.51cto.com/12970787/1955574
第11篇-JAVA IO