Java之I/O流
學習IO一直零零散散的,今天特地看教程、翻閱資料做筆記整理下I/O這塊
IO流用來處理裝置間的資料轉送。Java程式用“流”的方式進行資料輸入、輸出操作。java.io包下提供各種“流”類和介面擷取不同種類的資料,並通過標準的方法輸入輸出資料。
資料流的API結構
流的分類
IO流體系
>位元組流和字元流
位元組流<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPrbByKHKsaOstsG1vdK7uPbX1r3avs23tbvY0ru49tfWvdqjrL/J0tS0psDty/nT0MDg0M3K/b7do6zI587Esb6hos28xqyhom1wM6GiYXZptcijuzxiciAvPg0KPHN0cm9uZz7X1rf7wfc8L3N0cm9uZz7E3LbBtb224Lj219a92qOo1tDOxLbU06bX1r3ayv3KxzK49qOsVVRGLTjC67HtysczuPbX1r3ao6mjrM/IsunWuLaoseDC67Hto6y9q7Lptb3X1rf7t7W72KGj1rvE3LSmwO3X1rf7yv2+3aOstKbA7bS/zsSxvsr9vt3Txc/Iv7zCx8q508PX1rf7wfejrMbky/vA4NDNyv2+3dPD19a92sH3oaM8YnIgLz4NCjxiciAvPg0KJmd0O73atePB9yjWsb3T1/fTw9PazsS8/rXEKbrNtKbA7cH3PGJyIC8+DQo8aW1nIGFsdD0="這裡寫圖片描述" src="http://www.bkjia.com/uploads/allimg/160409/04332S603-4.png" title="\" />
>四大抽象基類及常用方法
>檔案流(節點流)
"-FileInputStream |-FileOutPutStream
|-FileReadr |-FileWriter
範例程式碼
package com.afy.iodemo;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileInputOutputStreamDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("a.gif"); FileOutputStream fos = new FileOutputStream("b.gif"); byte input[] = new byte[50]; while (fis.read(input)!=-1) { fos.write(input); } fis.close(); fos.close(); System.out.println("done"); }}
package com.afy.iodemo;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FileReaderWriterDemo { public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; char c[] = null; int len; try { File src = new File("yoyo.txt"); File dest = new File("yoyo1.txt"); fr = new FileReader(src); fw = new FileWriter(dest); c = new char[40]; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { while ((len=fr.read(c))!=-1) { fw.write(c,0, len); } fw.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } }}
>緩衝流
|-BufferedInputStream |-BufferedOutputStream
|-BufferedReader |-BufferedWriter
屬於處理流的一種,可以提高讀寫效率
範例程式碼:
package com.afy.iodemo;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class BufferedInputOutputDemo { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("reread.wmv"); BufferedInputStream bis = new BufferedInputStream(fis,1000000); FileOutputStream fos = new FileOutputStream("newmovie.wmv"); BufferedOutputStream bos = new BufferedOutputStream(fos,1000000); byte input[] = new byte[100000]; int count = 0; long before = System.currentTimeMillis(); while (bis.read(input)!=-1) { count++; bos.write(input); } System.out.println(count+"次"); System.out.println("讀取時間為:"+(System.currentTimeMillis()-before)+"ms"); bis.close(); fis.close(); bos.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
BufferedReader和BufferedWriter樣本與上例類似,其中BufferedReader提供readLine()方法來讀取一行字串,BufferedWriter雖有提供write(String sr)方法,但無法寫出一行字串,只能用列印流PrintWriter中的println(String x)方法寫出一行字串。要注意的是在使用緩衝流寫出資料的時候,當寫出的資料還沒填滿緩衝區時,緩衝區裡的內容有時候是不會寫到檔案裡的,會造成在寫出檔案時檔案部分丟失,解決方案是在完成寫出操作後調用相應流的fluch()方法將當前緩衝區所有的內容進行強制輸出。
>轉換流
|-InputStreamReader 位元組流轉字元流
|-OutputStreamWriter 字元流轉位元組流
首先它們是字元流,然後InputStreamReader的read()方法沒有提供讀取一個字串的方法,直接讀取一個或一行字串會變得很麻煩,解決方案是BufferedReader的readLine()方法,一次讀一行,到行標記時,將行標記之前的字元資料作為字串返回。當讀到末尾時,返回null。然而在使用BufferedWriter中的write(String,str)寫出字串時卻並沒有寫出換行的字串;同樣,OutputStreamWriter的write()方法中雖然有write(String str)方法,但也無法寫出一行字串,這樣寫出的文本沒法換行。解決方案是PrintWriter類(列印流)中的println(String x)方法,
>痛點易混淆點
|->BufferedReader中的readLine()方法
|->PrintWriter中的println(String x)<方法
|->寫出檔案後添加的相應流的flush()方法
>遺留問題
|-位元組流的read()方法讀取一個位元組為什麼返回的是int類型,而不是byte類型?
|-write()方法一次寫入一個位元組,接收的是一個int類型的數值,但只保留原位元組,並在剩餘二進位位補0?
^_^你造這是神馬鬼嗎?Please tell me,thx!