Java學習筆記40(緩衝流)

來源:互聯網
上載者:User

標籤:++   測試   char   buffer   toc   輸出   sys   文字檔   問題   

緩衝流:

在讀寫檔案的各種流中,最令人煩惱的就是效率問題,

而緩衝流的目的就是提高讀寫效率

 

位元組輸出緩衝流:

package demo;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;//提高寫入效率public class BufferedOutputStreamDemo {    public static void main(String[] args) throws IOException {        FileOutputStream fos = new FileOutputStream("d:\\buffer.txt");        BufferedOutputStream bos = new BufferedOutputStream(fos);        bos.write(66);        byte[] bytes = "HelloWorld".getBytes();        bos.write(bytes);        bos.close();    }}

 

位元組輸入緩衝流:

package demo;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;public class BufferedInputStreamDemo {    public static void main(String[] args) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\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();    }}

 

可以利用緩衝流複製檔案,和以前的方法做對比:

並且比較下複製時間

package demo;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Copy {    public static void main(String[] args) throws IOException {        long s = System.currentTimeMillis();        copy1(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));        long e = System.currentTimeMillis();        System.out.println(e - s);// 複製了14154毫秒(14秒)        copy2(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));        // 同樣的方法測試時間:129毫秒(0.1秒)        copy2(new File("d:\\LOL.exe"), new File("e:\\LOL.exe"));        // 測試時間:94毫秒(不到0.1秒)    }    public static void copy1(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();    }    public static void copy2(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();    }    public static void copy3(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 * 10];        while ((len = bis.read(bytes)) != -1) {            bos.write(bytes, 0, len);        }        bos.close();        bis.close();    }}

 

 

字元緩衝輸出資料流:

package demo;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterDemo{    public static void main(String[] args) throws IOException {        write();    }    public static void write() throws IOException{        FileWriter fw = new FileWriter("d:\\buffer.txt");        BufferedWriter bfw1 = new BufferedWriter(fw);        bfw1.write(100);        bfw1.flush();        bfw1.write("你好".toCharArray());        bfw1.newLine();//特有換行方法        //可以用\r\n換行,不過建議使用這種方法,具有平台無關性        bfw1.flush();        bfw1.write("HelloWorld");        bfw1.flush();        bfw1.close();            }}

 

 

字元緩衝輸入資料流:

package demo;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class BufferedReaderDemo {    public static void main(String[] args) throws IOException {        read();    }    public static void read() throws IOException {        int LineNumber = 0;        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\read.txt"));        // 緩衝流特有方法,讀取文本單行        String line = null;        while ((line = bfr1.readLine()) != null) {            LineNumber++;            System.out.println("第" + LineNumber + "行的內容:" + line);        }        bfr1.close();    }}

 

字元緩衝流複製文字檔:

package demo;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Copy {    public static void main(String[] args) throws IOException {        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\read.txt"));        BufferedWriter bfw1 = new BufferedWriter(new FileWriter("e:\\read.txt"));        String line = null;        while ((line = bfr1.readLine()) != null) {            bfw1.write(line);            bfw1.newLine();            bfw1.flush();        }        bfw1.close();        bfr1.close();    }}

 

 

關於各種流的操作規律和選用:

1.明確是要讀取還是寫入(源和目的)

2.明確是要操作什麼類型的,位元組還是文本?

3.明確資料所在的裝置,在硬碟中還是記憶體中,或者是網路?(這裡還沒有介紹記憶體流和socket)

4.是否需要編碼轉換,需要利用緩衝流、數組提高效率碼?

 

Java學習筆記40(緩衝流)

聯繫我們

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