Java基礎IO流的簡單總結

來源:互聯網
上載者:User

一 IO流的三種分類方式:
              1.按照流向來分:

                     輸入資料流:只能從中讀取位元組資料,不能向其寫出資料
                     輸出資料流:只能向其寫入位元組資料,不能從中讀取資料

                    

              2.按照流所處理的資料類型劃分:可分為:

                     位元組流:用於處理位元組資料。

                     字元流:用於處理Unicode字元資料。

                    

              3.按照格式可以分為:

                     節點流(低級流):可以從/向一個特定的IO裝置(如磁碟,網路)讀/寫資料的流。

                     處理流(進階流):可以對一個已存在的流的串連和封裝,通過所封裝的流的功能實現資料讀寫功能的流。

二 IO流的四大抽象類別:               
              1.常見InputStream類(讀資料)
                     -低級

                            InputStream
FileInputStream
                            ByteArrayInputStream

                            PipedInputStream

                     -進階

                            DataInputStream

                            BufferedInputStream

                    

              2.常見OutputStream(寫資料)

                     -低級

                            OutputStream
                            FileOutputStream

                            ByteArrayOutputStream

                            PipedOutputStream

                     -進階

                            DataOutputStream

                            BufferedOutputStream

                    

              3.常見的Reader類

                     -低級

                            CharArrayReader

                            StringReader

                            PipedReader

                            FileReader
                     -進階

                            BufferedReader
                            InputStreamReader
                            LineNumberReader

                    

              4.常見的Writer相關類
                     -低級

                            CharArrayWriter

                            StringWriter

                            PipedWriter

                            FileWriter

                     -進階

                            BufferedWriter

                            OutputStreamWriter

                            PrintWriter
                           

              注意:所有進階流都不能直接IO裝置(磁碟或網路等)進行直接的互動,必須建立在低級流的基礎之上。
      

三 緩衝流:
       BufferedReader   ->Reader

       BufferedWriter

       BufferedInputStream  ->InputStream

       BufferedOutputStream

      

 緩衝流輸入資料流 支援其父類的mark()和reset()方法。

       mark()方法用於“標記”當前位置,就像加入了一個書籤,可以使reset()方法返回這個標記重新讀取資料。

      

       BufferedReader  -readLine()  --以/r或/n分隔

       BufferedWriter  -newLine()  --寫入一個行分隔字元

      

       BufferInputStream和BufferedOutputStream平時很少用到。

      

四 InputStream的基本方法
       1.int read() throws IOException  讀取一個位元組以整數形式返回,如果返回-1證明讀到檔案的末尾。

              int read(byte[] buffer) throws IOException 將資料讀入一個位元組數組,同時返回讀取的位元組數。

              int read(byte[] buffer,int offset,int length) throws IOException 將資料讀入一個位元組數組,放到數組的offset指定的位置開始,並用length來制定讀取的最大位元組數。

       2.void close() throws IOException 關閉流釋放記憶體資源
       3.int available()返回可以ong種讀取的位元組數。

       4.long skip(long n)在輸入資料流中跳過n個位元組,將實際跳過的位元組數返回

       5.boolean markSupported() 判斷流是否支援標記功能。

       6.void mark(int readlimit)在支援標記的輸入資料流的當前位置設定一個標記。

       7.void reset() 返回到流的上一個標記。注意必須支援標記功能。

      

五 OutputStream的基本方法
       1.viod write(int b)throws IOException向輸出資料流寫入一個位元組資料。

              void write(byte[] buffer)

              void write(byte[] buffer,int offset,int length)

       2.viod flush() 將輸出資料流中緩衝的資料全部寫出到目的地。

       3.void close() 關閉流
      

六 Writer的基本方法
       1.void write(int c) throws IOException 向輸出資料流中寫入一個字元資料。
         void write(char[] buffer) 將字元數組buffer中的字元寫入到流中

         void write(char[] buffer,int offset,int length) 將字元數組buffer中從offset開始的length個字元寫入到流中

         void write(String str) throws IOException 將一個字串中的字元寫入到輸出資料流
         void write(String str,int offset,int length)將一個字串從offset開始的length個字元寫入到輸出資料流

       4.void flush() 將輸出資料流中緩衝的資料全部寫出到目的地。

      

七 Reader的基本方法
       1.int read() 讀取一個字元以整數形式返回,如果返回-1證明已到流的末尾。

         int read(char[] buffer)將從流中讀出的字元放到字元數組buffer中,返回讀出的字元數。

         int read(char[] buffer,int offset,int length) 將讀出的字元放到字元數組的指定offset開始的空間,每次最多讀出length個字元。

       2.void close();
       3.skip(long n)

       4.void mark(int readlimit)

       5.void reset()

      

八 訪問檔案類
       1.FileInputStream和FileOutputStream繼承基類用於向檔案中輸入輸出位元組
       2.FileReader和FileWriter繼承基類用於向檔案中輸入輸出字元。
              注意:a.輸出資料流在建構函式第二個參數可以設定true,表示在檔案的末尾進行追加寫入。
                       b.此類流會拋出FileNotFoundException異常。
                                  

九 轉換流:主要作用是將位元組流轉換為字元流。
       1.InputStreamReader需要和InputStream套接
       2.OutputStreamWriter需要和OutputStream套接
      

十 資料流與位元組數組流:
       1.資料流主要為實現可以存取java未經處理資料類型,如long,boolean等

              資料流是位元組流

       2.DataInputStream需要和InputStream套接
              DataOutputStream需要和OutputStream套接
       3.DataInputStream的方法:

              -readBoolean();readInt();read...

              -readUTF()網路傳輸常用方法,讀一個Unicode字串

       4.DataOutputStream方法與DataInputStream中的方法對應

       5.//此建構函式等於已可以往一個位元組數組裡輸入內容

              ByteArrayOutputStream baos = new ByteArrayOutputStream();

              //此方法為擷取一個位元組數組方法返回位元組數組

              baos.toByteArray();

              //此方法擷取位元組數組佔了多少位元組

              new ByteArrayInputStream(byte[]).available()

             

十一 Print流
       1.Print流只有輸出資料流沒有輸入資料流,PrintWriter和PrintStream分別針對字元和位元組
       2.提供print和println方法,用於多種資料類型的輸出。
       3.輸出操作不會拋出異常。
       4.PrintWriter和PrintStream可設定自動重新整理功能。

      

十二 Object流
       1.序列化,用於直接將Object寫入或者讀出。
       2.static和transient關鍵字修飾的屬性是不能被序列化的。
       3.需要序列化的類必須實現Serializable介面或者Externalizable
       4.方法:

              -writeObject(Object obj)
              -Object readObject();
              若想得到Object的原始類型,需要通過強制轉型。
             

十三 特殊的檔案流(RandomAccessFile)
       1.RandomAccessFile是一種特殊的檔案流,可以用它在檔案的任何地方尋找或插入資料

       2.RandomAccessFile同時實現了DataInput和DataOutput介面,所以可以用它來讀寫檔案

       3.構造器

              -RandomAccessFile(File file,String mode)

              -RandomAccessFile(String file,String mode)

       4.利用RandomAccessFile來追加檔案內容:

              RandomAccessFile rf1 = new RandomAccessFile("c://1.txt","rw");

              rf1.seek(rf1.length());

              rf1.writeBytes(str + "/n");  //此處若想輸入的文本看起來是換行的,需加/n/r

              rf1.close();

             

              隨機檔案這塊同學們可以自己試試。

十四 讀取檔案匹配流
1.       FileInputStream/FileOutputStream
2.       FileReader/FileWriter
3.       BufferedReader/PrintWriter
             

       **************************************
       決定使用哪個類,以及構造進程的一般原則是:
              1.首先考慮原始的資料格式是什嗎?
                     a.二進位格式(只要不能確定是純文字的): InputStream, OutputStream及其所有帶Stream結束的子類

                     b.純文字格式(含純英文與漢字或其他編碼方式);Reader, Writer及其子類

              2.考慮是輸入還是輸出?
                     a.輸入:Reader, InputStream類型的子類

                     b.輸出:Writer, OutputStream類型的子類

              3.是否需要通過中介?
                     a.從Stream到Reader,Writer的轉換類:InputStreamReader, OutputStreamWriter

                     b.低級流到進階流的轉換

              4.資料的來源或者去向是什嗎?
                     a.是檔案: FileInputStream, FileOutputStream, FileReader, FileWriter

      b.是byte[]:ByteArrayInputStream, ByteArrayOutputStream

                     c.是Char[]: CharArrayReader, CharArrayWriter

                     d.是String: StringBufferInputStream, StringReader, StringWriter

                     e.網路資料流:InputStream, OutputStream, Reader, Writer

              5.是否要緩衝?

                     a.若需要就是四種緩衝類:BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter

              6.是否要格式化輸出?
                     a.PrintStream, PrintWriter

 

聯繫我們

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