JAVA中inputStream/OutputStream位元組輸入資料流、輸出資料流讀寫檔案__JAVA

來源:互聯網
上載者:User

好記性不如賴筆頭……
注意:InputStream/OutputStream是抽象類別,不能被執行個體化,只能執行個體化其子類,且是位元組流,不是字元流

InputStream is = new FileInputStream(“a.txt”); 等同於 InputStream is = new FileInputStream(new File(“a.txt”));

OutputStream類似與之類似,

package com.Ckinghan.outputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class OutputStreamDemo {    public static void main(String[] args) {        /**         * 使用位元組流寫入檔案         */        outputStreadRead();        /**         *位元組流的資料讀取方式一:每次讀取一個位元組         */        inputStreamReader();        /**         * 位元組流的資料讀取方式二:每次讀取一定長度的位元組,建議使用         */        inputStreamReader1();        /**         * 位元組流的資料讀取方式三:每次讀取一定長度的位元組從指定的數組索引上儲存資料         */        inputStreamReader2();    }    /**     * @描述:位元組流的資料讀取方式三:每次讀取一定長度的位元組從指定的數組索引上儲存資料     * @建立時間:     */    public static void inputStreamReader2(){        //建立位元組對象        InputStream inputStream = null;        try {            //執行個體化對象            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");            /**             * 每次讀取一定長度的位元組從指定的數組索引上儲存資料,可以讀取中文(檔案中的資料為:OutputStreamp測試寫入資料)             */            //定義每次讀取位元組的大小與儲存位元組的資料            byte[] bs = new byte[1024];            //定義每次讀取的長度            int len = -1;            /**             * inputStream.read(bs,1,10); 參數說明:             * bs:數組,每次最大可以儲存1024個位元組             * 1    :指定的索引位置,這裡是從bs數組的索引1個開始儲存資料             * 10:從inputStream讀取的位元組個數,從0開始,注意,如果讀取的資料中文,而讀取的位元組個數為奇數,可能會現無法識別或錯誤的資料             */            len = inputStream.read(bs,1,10);            //輸出字元,注意:在輸出時,應從指定的索引位置進行讀取            System.out.println(new String(bs,1,len));         } catch (FileNotFoundException e) {            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        }finally {            //關閉流            if(inputStream != null){                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * @描述:位元組流的資料讀取方式二:每次讀取一定長度的位元組,建議使用     * @建立時間:     */    public static void inputStreamReader1(){        //建立位元組對象        InputStream inputStream = null;        try {            //執行個體化對象            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");            /**             * 讀取一定長度的位元組資料,建議使用,可以讀取中文(檔案中的資料為:OutputStreamp測試寫入資料)             * 讀取結果為:OutputStreamp測試寫入資料             */            //定義每次讀取位元組的大小與儲存位元組的資料            byte[] bs = new byte[1024];            //定義每次讀取的長度            int len = -1;            //迴圈讀取資料,如果讀取的資料為-1,說明已經讀取了末尾            while((len = inputStream.read(bs)) != -1){                //輸出字元                System.out.println(new String(bs,0,len));             }        } catch (FileNotFoundException e) {            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        }finally {            //關閉流            if(inputStream != null){                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * @描述:位元組流的資料讀取方式一:每次讀取一個位元組     * @建立時間:     */    public static void inputStreamReader(){        //建立對象        InputStream inputStream = null;        try {            //執行個體化對象            inputStream  = new FileInputStream("JavaIOOutputStreamReader.java");            /**             * 每次讀取一個位元組,對於英文是可以的(檔案中的資料為:OutputStreamp測試寫入資料),             * 但不能讀取中文,因為編碼我使用的是UTF-8,一個中文佔兩個位元組的長度             * 讀取的結果如下:OutputStreamp????????????             */            //建立讀取的字元,儲存的對應的acsii碼            int b = -1;            //迴圈讀取字元,如果字元為-1,說明已讀取到檔案的末尾            while ((b = inputStream.read()) != -1) {                //將讀取的acsii碼轉換為字元並輸出                System.out.print((char)b);            }            System.out.println();        } catch (FileNotFoundException e) {            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        }finally {            //關閉流            if(inputStream != null){                try {                    inputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * @描述:使用位元組流寫入資料到檔案中     * @建立時間:     */    public static void outputStreadRead(){        //建立位元組流對象        OutputStream stream = null;        try {            //執行個體化對象            stream = new FileOutputStream("JavaIOOutputStreamReader.java");            //要寫入的字串資料            String string = "OutputStreamp測試寫入資料";            //將字串資料轉換為位元組數組            byte[] bytes = string.getBytes();            //將位元組數組寫入到檔案            stream.write(bytes);            //清空緩衝區,將寫入的資料儲存            stream.flush();            //寫入成功後的提示            System.out.println("寫入檔案成功");            //拋出異常        } catch (IOException e) {            e.printStackTrace();        }finally {            //如果stream被執行個體化            if(stream != null){                try {                    //關閉位元組流                    stream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.