InputStream 、 InputStreamReader 、 BufferedReader區別

來源:互聯網
上載者:User

標籤:def   返回   fileinput   方式   txt   字元   stat   輸出資料流   stream   

區別介紹:
1、InputStream、OutputStream

處理位元組流的抽象類別

InputStream 是位元組輸入資料流的所有類的超類,一般我們使用它的子類,如FileInputStream等.

OutputStream是位元組輸出資料流的所有類的超類,一般我們使用它的子類,如FileOutputStream等.

?

2、InputStreamReader? OutputStreamWriter

處理字元流的抽象類別

InputStreamReader 是位元組流通向字元流的橋樑,它將位元組流轉換為字元流.

OutputStreamWriter是字元流通向位元組流的橋樑,它將字元流轉換為位元組流.

?

3、BufferedReader BufferedWriter

BufferedReader 由Reader類擴充而來,提供通用的緩衝方式文本讀取,readLine讀取一個文本行,

從字元輸入資料流中讀取文本,緩衝各個字元,從而提供字元、數組和行的高效讀取。

BufferedWriter??由Writer?類擴充而來,提供通用的緩衝方式文本寫入, newLine使用平台自己的行分隔字元,

將文本寫入字元輸出資料流,緩衝各個字元,從而提供單個字元、數組和字串的高效寫入。

==============================================================

E:/baoyue13.txt 檔案內容:

123456789abcdefg235xyz

附代碼:

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;public class FileInputStreamDemo {    public static void main(String[] args) throws Exception {        /**         * InputStream能從來源處讀取一個一個byte,所以它是最低級的,         */        // read()方法返回的int類型,是表示資料下一個位元組的位元組碼,如果已經到達流的最後面了,那就返回-1        // 通過char 強轉,輸出位元組碼的內容        FileInputStream fileIn2 = new FileInputStream(new File("E:/baoyue13.txt"));        int read = fileIn2.read();        System.out.println( read);        System.out.println((char) read);        byte[] bb = new byte[4];        int read2 = fileIn2.read(bb);        System.out.println( "長度 read2: " + read2);        for(byte b:bb) {            System.out.println(b);            System.out.println((char)b);        }        int aa;        while ((aa = fileIn2.read()) != -1) {            System.out.print((char) aa);        }        System.out.println();        System.out.println("--------------------------------");        /**         * InputStreamReader封裝了InputStream在裡頭,它以較進階的方式,一次讀取一個一個字元,         *          */        FileInputStream fileIn3 = new FileInputStream(new File("E:/baoyue13.txt"));        InputStreamReader sr = new InputStreamReader(fileIn3);        System.out.println((char)sr.read());        int a2;        while ((a2 = sr.read()) != -1) {            System.out.print((char) a2);        }        System.out.println();        System.out.println("--------------------------------");        /**         * BufferedReader則是比InputStreamReader更進階,         * 它封裝了StreamReader類,一次讀取取一行的字元         *          */        FileInputStream fileIn4 = new FileInputStream(new File("E:/baoyue13.txt"));        InputStreamReader sr2 = new InputStreamReader(fileIn4);        BufferedReader br=new BufferedReader(sr2);         String line;        while ((line = br.readLine()) != null) {            System.out.println(line);        }    }}

執行結果:

491長度 read2: 45025135245356789abcdefg235xyz--------------------------------123456789abcdefg235xyz--------------------------------123456789abcdefg235xyz

InputStream 、 InputStreamReader 、 BufferedReader區別

聯繫我們

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