java IO流之三 使用緩衝流來讀寫檔案

來源:互聯網
上載者:User

標籤:style   stream   import   讀寫檔案   通過   高效   redo   輸出資料流   close   

原文:http://blog.csdn.net/haluoluo211/article/details/52183219

一、通過BufferedReader和BufferedWriter來讀寫檔案

 

     使用緩衝流的好處是,能夠更高效的讀寫資訊,原理是將資料先緩衝起來,然後一起寫入或者讀取出來。經常使用的是readLine()方法,表示一次讀取一行資料。

package org.example.io;    import java.io.BufferedReader;  import java.io.BufferedWriter;  import java.io.File;  import java.io.FileNotFoundException;  import java.io.FileReader;  import java.io.FileWriter;  import java.io.IOException;    public class TestBufferedWriter {        public static void main(String[] args) throws Exception {          write();          read();      }        /**      * DOC 讀取資訊.      *       * @throws FileNotFoundException      * @throws IOException      */      private static void read() throws FileNotFoundException, IOException {          File file = new File("E:\\a.txt");// 指定要讀取的檔案          // 獲得該檔案的緩衝輸入資料流          BufferedReader bufferedReader = new BufferedReader(new FileReader(file));          String line = "";// 用來儲存每次讀取一行的內容          while ((line = bufferedReader.readLine()) != null) {              System.out.println(line);          }          bufferedReader.close();// 關閉輸入資料流      }        /**      * DOC 寫入資訊.      *       * @throws IOException      */      private static void write() throws IOException {          File file = new File("E:\\a.txt");// 指定要寫入的檔案          if (!file.exists()) {// 如果檔案不存在則建立              file.createNewFile();          }          // 擷取該檔案的緩衝輸出資料流          BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));          // 寫入資訊          bufferedWriter.write("你好世界");          bufferedWriter.newLine();// 表示換行          bufferedWriter.write("hello world");          bufferedWriter.flush();// 清空緩衝區          bufferedWriter.close();// 關閉輸出資料流      }    }  

 

二、使用BufferedInputStream和BufferedOuputStream讀寫圖片

 

使用方式和FileInputStrem和FileOutputStream基本一致:

 

package org.example.io;    import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;    public class TestBufferedString {        public static void main(String[] args) throws Exception {          // 指定要讀取檔案的緩衝輸入位元組流          BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\test.jpg"));          File file = new File("E:\\test.jpg");          if (file != null) {              file.createNewFile();          }          // 指定要寫入檔案的緩衝輸出位元組流          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));          byte[] bb = new byte[1024];// 用來儲存每次讀取到的位元組數組          int n;// 每次讀取到的位元組數組的長度          while ((n = in.read(bb)) != -1) {              out.write(bb, 0, n);// 寫入到輸出資料流          }          out.close();// 關閉流          in.close();      }    }  

 

java IO流之三 使用緩衝流來讀寫檔案

聯繫我們

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