java bio總結

來源:互聯網
上載者:User

標籤:nts   寫文本   ace   stat   目標   while   except   關閉   lin   

、同步非同步、阻塞非阻塞(目前不是很清楚,這篇部落格寫完後,後續進行處理)

  1.同步和非同步:關注的是訊息的通訊機制,

    同步:發起調用後,如果沒有得到結果,該調用是不會返回的;該調用者會主動等待調用返回。

    非同步:發起調用後,調用立刻返回,但並不是返回的結果;也就是說調用者並不會立刻得到結果,而是被調用者通知調用者,或者通過回呼函數處理這個調用。

  2.阻塞和非阻塞:指的是程式等待調用結果時的狀態。只有同步才會有阻塞和非阻塞的概念。

    阻塞:發起調用後當前線程會被掛起,直到有返回

    非阻塞:發起調用後,當前線程不會被掛起,而是繼續執行。

 

二、io流的分類

  1.輸入資料流、輸出資料流:根據流是否流向處理器來確定是輸入資料流還是輸出資料流。

  2.位元組流、字元流:位元組流讀到的資料不會進行任何處理;字元流讀取到的位元據會根據相應的編碼進行轉換為字元,字元流=位元組流+解碼。

三、io流類結構圖

 

四、常用功能

  注意一點:使用完io後,一定要關閉流

 1.io流對檔案進行讀取

/** * 字元流讀寫文字檔  * 在使用io讀寫文字檔的時候一定要記住關閉流 */public class Test2 {    public static void main(String[] args) {        ReadFile(new File("E:/b.txt"));        WriteFile(new File("E:/c.txt"));    }        public static void ReadFile(File file){        FileReader fr = null;        BufferedReader br = null;        InputStreamReader isr = null;        try {                        System.out.println("\r\n========自己定義緩衝數組來讀取檔案================");            fr = new FileReader(file);            char[] buffer1 = new char[3];            int len1 = 0;            while((len1 = fr.read(buffer1)) != -1){                System.out.print(new String(buffer1,0,len1));            }                        System.out.println("\r\n========使用緩衝流來讀取檔案================");            br = new BufferedReader(new FileReader(file));            String temp = "";            while((temp = br.readLine()) != null){                System.out.println(temp);            }                        System.out.println("\r\n========使用轉換流來讀取檔案===========");            isr = new InputStreamReader(new FileInputStream(file),"utf-8");            char[] buffer2 = new char[1];            int len2 = 0;            while((len2 = isr.read(buffer2)) != -1 ){                System.out.print(new String(buffer2,0,len2));            }                    } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            try {                br.close();                isr.close();            } catch (IOException e) {                e.printStackTrace();            }        }            }        public static void WriteFile(File file){        FileOutputStream fos = null;        FileWriter fw = null;        BufferedWriter bw = null;        try {            //========使用位元組流寫入檔案===========            fos = new FileOutputStream(file);            fos.write("測試寫入檔案abc123。。".getBytes());            fos.write("測試是否疊加".getBytes());            fos.flush();            //========使用字元流寫入檔案===========            fw = new FileWriter(file,true);            fw.write("\r\n========使用字元流寫入檔案===========");            fw.write("這是用FileWriter寫入的資料 ");            fw.flush();            //========使用緩衝流寫入檔案===========            //FileWriter預設是會覆蓋上一次的資料,所以在建立的時候要將是否追加參數設定為true            bw = new BufferedWriter(new FileWriter(file,true));            bw.newLine();            bw.write("使用緩衝流寫入");            bw.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (NumberFormatException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            try {                bw.close();                fw.close();                fos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}

 2.位元組流對檔案進行複製

  

/** * 位元組流對檔案進行複製* */public class Test1 {    public static void main(String[] args) throws IOException {        copy();    }        public static void copy(){        FileInputStream fis = null;        FileOutputStream fos = null;        try{            fis = new FileInputStream("E:/a.txt");            fos = new FileOutputStream("E:/b.txt");            byte[] buffer = new byte[1024];//使用數組進行緩衝,提高io速度            int len = 0;            while((len = fis.read(buffer)) != -1){                fos.write(buffer, 0, len);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            try {                fos.close();            } catch (IOException e) {                e.printStackTrace();            }            try {                fis.close();            } catch (IOException e) {                e.printStackTrace();            }                    }    }}

 

五、IO流使用的一些細節

  1.IO流中,緩衝類都是Buffered*類,其並不具備讀寫能力,只是內部維護了一個8kb的數組,只有在調用了close()和flush()或者位元組數組已經滿了才會將資料寫入到硬碟。提高了檔案的讀寫效率。

  2.檔案輸出資料流,目標檔案如果不存在,則會建立檔案。其write方法預設清空檔案內容,再寫入內容,需要在構造方法中設定才能成功。

  3.FileOutputStream 參數為int時,只會把低8位寫出,剩餘24位丟棄。

  4.轉換流的作用就是將位元組流轉換為字元流,並可以按照指定的編碼對資料進行讀寫。

 

java bio總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.