字節(jié)輸出流緩沖流BufferedOutputStream字節(jié)輸入流緩沖流BufferedInputStream

08字節(jié)輸出流緩沖流BufferedOutputStream

* A: 字節(jié)輸出流緩沖流BufferedOutputStream
    * a: BufferedOutputStream
        * 字節(jié)輸出流的緩沖流
        * java.io.BufferedOuputStream 作用: 提高原有輸出流的寫入效率
        * BufferedOuputStream 繼承 OutputStream
        * 方法,寫入 write 字節(jié),字節(jié)數(shù)組            
        * 構(gòu)造方法:
            * BufferedOuputStream(OuputStream out)
            * 可以傳遞任意的字節(jié)輸出流, 傳遞的是哪個字節(jié)流,就對哪個字節(jié)流提高效率  

    * b: 案例代碼
        public class BufferedOutputStreamDemo {
            public static void main(String[] args)throws IOException {
                //創(chuàng)建字節(jié)輸出流,綁定文件
                //FileOutputStream fos = new FileOutputStream("c:\\buffer.txt");
                //創(chuàng)建字節(jié)輸出流緩沖流的對象,構(gòu)造方法中,傳遞字節(jié)輸出流
                BufferedOutputStream bos = new
                        BufferedOutputStream(new FileOutputStream("c:\\buffer.txt"));
                
                bos.write(55);
                
                byte[] bytes = "HelloWorld".getBytes();
                
                bos.write(bytes);
                
                bos.write(bytes, 3, 2);
                
                bos.close();
            }
        }

09字節(jié)輸入流緩沖流BufferedInputStream

* A: 字節(jié)輸入流緩沖流BufferedInputStream
    * a: BufferedInputStream
        * 字節(jié)輸入流的緩沖流
        * 繼承InputStream,標(biāo)準(zhǔn)的字節(jié)輸入流
        * 讀取方法  read() 單個字節(jié),字節(jié)數(shù)組              
        * 構(gòu)造方法:
            * BufferedInputStream(InputStream in)
            * 可以傳遞任意的字節(jié)輸入流,傳遞是誰,就提高誰的效率
            * 可以傳遞的字節(jié)輸入流 FileInputStream
    * b: 案例代碼
        public class BufferedInputStreamDemo {
            public static void main(String[] args) throws IOException{
                //創(chuàng)建字節(jié)輸入流的緩沖流對象,構(gòu)造方法中包裝字節(jié)輸入流,包裝文件
                BufferedInputStream bis = new 
                        BufferedInputStream(new FileInputStream("c:\\buffer.txt"));
                byte[] bytes = new byte[10];
                int len = 0 ;
                while((len = bis.read(bytes))!=-1){
                    System.out.print(new String(bytes,0,len));
                }
                bis.close();
            }
        }

10四種文件復(fù)制方式的效率比較

* A:四種文件復(fù)制方式的效率比較
    * a: 四中復(fù)制方式
        * 字節(jié)流讀寫單個字節(jié)                    125250 毫秒
        * 字節(jié)流讀寫字節(jié)數(shù)組                    193    毫秒  OK
        * 字節(jié)流緩沖區(qū)流讀寫單個字節(jié)         1210   毫秒
        * 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組            73     毫秒  OK        
                
    * b: 案例代碼
    
        public class Copy {
            public static void main(String[] args)throws IOException {
                long s = System.currentTimeMillis();
                copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
                long e = System.currentTimeMillis();
                System.out.println(e-s);
            }
            /*
             * 方法,實現(xiàn)文件復(fù)制
             *  4. 字節(jié)流緩沖區(qū)流讀寫字節(jié)數(shù)組
             */
            public static void copy_4(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                bos.close();
                bis.close();
            }
            /*
             * 方法,實現(xiàn)文件復(fù)制
             *  3. 字節(jié)流緩沖區(qū)流讀寫單個字節(jié)
             */
            public static void copy_3(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                while((len = bis.read())!=-1){
                    bos.write(len);
                }
                bos.close();
                bis.close();
            }
            
            /*
             * 方法,實現(xiàn)文件復(fù)制
             *  2. 字節(jié)流讀寫字節(jié)數(shù)組
             */
            public static void copy_2(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = fis.read(bytes))!=-1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }
            
            /*
             * 方法,實現(xiàn)文件復(fù)制
             *  1. 字節(jié)流讀寫單個字節(jié)
             */
            public static void copy_1(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                while((len = fis.read())!=-1){
                    fos.write(len);
                }
                fos.close();
                fis.close();
            }
        }

11字符輸出流緩沖流BufferedWriter

* A: 字符輸出流緩沖流BufferedWriter
    * a: BufferedWriter
        * 字符輸出流緩沖區(qū)流
        * java.io.BufferedWriter 繼承 Writer
        * 寫入方法 write () 單個字符,字符數(shù)組,字符串

        * 構(gòu)造方法:
            * BufferedWriter(Writer w)傳遞任意字符輸出流
            * 傳遞誰,就高效誰
            * 能傳遞的字符輸出流 FileWriter, OutputStreamWriter
    * b: 案例代碼
        public class BufferedWrierDemo {
            public static void main(String[] args) throws IOException{
                //創(chuàng)建字符輸出流,封裝文件
                FileWriter fw = new FileWriter("c:\\buffer.txt");
                BufferedWriter bfw = new BufferedWriter(fw);
                
                bfw.write(100);
                bfw.flush();
                bfw.write("你好".toCharArray());
                bfw.flush();
                
                
                bfw.write("你好");
                
                bfw.flush();
                
                
                bfw.write("我好好");
                
                bfw.flush();

                bfw.write("大家都好");
                bfw.flush();
                
                bfw.close();
                
            }
        }

12字符輸出流緩沖流BufferedWriter特有方法newLine

* A: 字符輸出流緩沖流BufferedWriter特有方法newLine
    * a: 方法介紹
        * void  newLine() 寫換行
            
        * newLine()文本中換行, \r\n也是文本換行
        * 方法具有平臺無關(guān)性
        * Windows  \r\n
        * Linux    \n
             
        * newLine()運行結(jié)果,和操作系統(tǒng)是相互關(guān)系
        * JVM: 安裝的是Windows版本,newLine()寫的就是\r\n
        * 安裝的是Linux版本,newLine()寫的就是\n
        /*
         *  將數(shù)據(jù)源 c:\\a.txt
         *  復(fù)制到 d:\\a.txt  數(shù)據(jù)目的
         *  字節(jié)輸入流,綁定數(shù)據(jù)源
         *  字節(jié)輸出流,綁定數(shù)據(jù)目的
         *  
         *  輸入,讀取1個字節(jié)
         *  輸出,寫1個字節(jié)
         */
    * b: 案例代碼
        public class BufferedWrierDemo {
            public static void main(String[] args) throws IOException{
                //創(chuàng)建字符輸出流,封裝文件
                FileWriter fw = new FileWriter("c:\\buffer.txt");
                BufferedWriter bfw = new BufferedWriter(fw);
                
                bfw.write(100);
                bfw.flush();
                bfw.write("你好".toCharArray());
                bfw.flush();
                
                bfw.write("你好");
                bfw.newLine();
                bfw.flush();
                
                
                bfw.write("我好好");
                bfw.newLine();
                bfw.flush();

                bfw.write("大家都好");
                bfw.flush();
                
                bfw.close();
                
            }
        }

13字符輸入流緩沖流BufferedReader

* A: 字符輸入流緩沖流BufferedReader
    * a: 概述
        * 從字符輸入流中讀取文本希停,緩沖各個字符陵且,從而實現(xiàn)字符撩满、數(shù)組和行的高效讀取
        * public String readLine() 讀取一個文本行奸焙,包含該行內(nèi)容的字符串,不包含任何行終止符先改,如果已到達(dá)流末尾,則返回 null

14字符輸入流緩沖流BufferedReader讀取文本行

* A: 字符輸入流緩沖流BufferedReader讀取文本行
    * a: BufferedReader
        * 字符輸入流緩沖流
        * java.io.BufferedReader 繼承 Reader
        * 讀取功能 read() 單個字符,字符數(shù)組
        * 構(gòu)造方法:
            * BufferedReader(Reader r)
            * 可以任意的字符輸入流
               FileReader  InputStreamReader       
        * BufferedReader自己的功能
            * String readLine() 讀取文本行 \r\n   
                * 方法讀取到流末尾,返回null
    * b: 小特點
         * 獲取內(nèi)容的方法一般都有返回值
         * int 沒有返回的都是負(fù)數(shù)
         * 引用類型 找不到返回null
         * boolean 找不到返回false
        
    * c: 案例代碼
        public class BufferedReaderDemo {
            public static void main(String[] args) throws IOException {
                int lineNumber = 0;
                //創(chuàng)建字符輸入流緩沖流對象,構(gòu)造方法傳遞字符輸入流,包裝數(shù)據(jù)源文件
                BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));
                //調(diào)用緩沖流的方法 readLine()讀取文本行
                //循環(huán)讀取文本行, 結(jié)束條件 readLine()返回null
                String line = null;
                while((line = bfr.readLine())!=null){
                    lineNumber++;
                    System.out.println(lineNumber+"  "+line);
                }
                bfr.close();
            }
        }

        /*
         * String line = bfr.readLine();
                System.out.println(line);
                
                line = bfr.readLine();
                System.out.println(line);
                
                line = bfr.readLine();
                System.out.println(line);
                
                line = bfr.readLine();
                System.out.println(line);
                
                line = bfr.readLine();
                System.out.println(line);
         */

15字符流緩沖區(qū)流復(fù)制文本文件

* A: 字符流緩沖區(qū)流復(fù)制文本文件
    * a: 案例代碼
        /*
         *  使用緩沖區(qū)流對象,復(fù)制文本文件
         *  數(shù)據(jù)源  BufferedReader+FileReader 讀取
         *  數(shù)據(jù)目的 BufferedWriter+FileWriter 寫入
         *  讀取文本行, 讀一行,寫一行,寫換行
         */
        public class Copy_1 {
            public static void main(String[] args) throws IOException{
                BufferedReader bfr = new BufferedReader(new FileReader("c:\\w.log"));   
                BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\\w.log"));
                //讀取文本行, 讀一行,寫一行,寫換行
                String line = null;
                while((line = bfr.readLine())!=null){
                    bfw.write(line);
                    bfw.newLine();
                    bfw.flush();
                }
                bfw.close();
                bfr.close();
            }
        }

16IO流對象的操作規(guī)律

* A: IO流對象的操作規(guī)律
    * a: 明確一:要操作的數(shù)據(jù)是數(shù)據(jù)源還是數(shù)據(jù)目的。
        * 源:InputStream    Reader
        * 目的:OutputStream Writer
        * 先根據(jù)需求明確要讀浩峡,還是要寫。

    * b: 明確二:要操作的數(shù)據(jù)是字節(jié)還是文本呢错敢?
        * 源:
            * 字節(jié):InputStream
            * 文本:Reader
        * 目的:
            * 字節(jié):OutputStream
            * 文本:Writer
    * c: 明確三:明確數(shù)據(jù)所在的具體設(shè)備翰灾。
        * 源設(shè)備:
            * 硬盤:文件  File開頭。
            * 內(nèi)存:數(shù)組稚茅,字符串纸淮。
            * 鍵盤:System.in;
            * 網(wǎng)絡(luò):Socket
        * 目的設(shè)備:
            * 硬盤:文件  File開頭。
            * 內(nèi)存:數(shù)組亚享,字符串咽块。
            * 屏幕:System.out
            * 網(wǎng)絡(luò):Socket
            * 完全可以明確具體要使用哪個流對象。
    * d: 明確四:是否需要額外功能呢虹蒋?
        * 額外功能:
            * 轉(zhuǎn)換嗎糜芳?轉(zhuǎn)換流。InputStreamReader OutputStreamWriter
            * 高效嗎魄衅?緩沖區(qū)對象峭竣。BufferedXXX
            * 已經(jīng)明確到了具體的體系上。

17總結(jié)

* 把今天的知識點總結(jié)一遍晃虫。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末皆撩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子哲银,更是在濱河造成了極大的恐慌扛吞,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荆责,死亡現(xiàn)場離奇詭異滥比,居然都是意外死亡,警方通過查閱死者的電腦和手機做院,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門盲泛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來濒持,“玉大人,你說我怎么就攤上這事寺滚「逃” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵村视,是天一觀的道長官套。 經(jīng)常有香客問我,道長蚁孔,這世上最難降的妖魔是什么奶赔? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮勒虾,結(jié)果婚禮上纺阔,老公的妹妹穿的比我還像新娘。我一直安慰自己修然,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布质况。 她就那樣靜靜地躺著愕宋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪结榄。 梳的紋絲不亂的頭發(fā)上中贝,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音臼朗,去河邊找鬼邻寿。 笑死,一個胖子當(dāng)著我的面吹牛视哑,可吹牛的內(nèi)容都是我干的绣否。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼挡毅,長吁一口氣:“原來是場噩夢啊……” “哼蒜撮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起跪呈,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤段磨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后耗绿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體苹支,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年误阻,在試婚紗的時候發(fā)現(xiàn)自己被綠了债蜜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晴埂。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖策幼,靈堂內(nèi)的尸體忽然破棺而出邑时,到底是詐尸還是另有隱情,我是刑警寧澤特姐,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布晶丘,位于F島的核電站,受9級特大地震影響唐含,放射性物質(zhì)發(fā)生泄漏浅浮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一捷枯、第九天 我趴在偏房一處隱蔽的房頂上張望滚秩。 院中可真熱鬧,春花似錦淮捆、人聲如沸郁油。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽桐腌。三九已至,卻和暖如春苟径,著一層夾襖步出監(jiān)牢的瞬間案站,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工棘街, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蟆盐,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓遭殉,卻偏偏與公主長得像石挂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子恩沽,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容

  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File類用于表示文件(目錄)...
    閆子揚閱讀 462評論 0 0
  • 1 IO(二)No19 【 緩沖流:內(nèi)置了緩沖區(qū)誊稚,對現(xiàn)有的流對象進行了封裝,實現(xiàn)了高效的讀寫操作并增強了功能 ...
    征程_Journey閱讀 712評論 0 1
  • 緩沖流是為了讀取和寫入的速度罗心,提高效率主要包括:字節(jié)緩沖流和字符緩沖流 字節(jié)緩沖流: 根據(jù)流的方向里伯,共有2個:字節(jié)...
    Mango_yes閱讀 580評論 0 0
  • 1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中渤闷,管道是一條不間斷的字節(jié)...
    JackChen1024閱讀 825評論 0 10
  • 親愛的***年級****班的各位家長: 新學(xué)年伊始疾瓮,為便于今后班級各項活動的順利開展,家委會通過微信群與部分家長進...
    谷舒羽閱讀 34,024評論 0 1