1. 程式人生 > 實用技巧 >Buffered緩衝流

Buffered緩衝流

Buffered緩衝流

示例程式碼:

/**
 * 緩衝流的使用
 * 1.緩衝流
 * BufferedInputStream
 * BufferedOutputStream
 * BufferedReader
 * BufferedWriter
 *
 * 2。作用:提高讀取和寫入的速度
 *  提高速度的原因:內部提供了一個緩衝區
 *
 * 3. 處理流就是套接在已有的流的基礎上
 */
public class BufferedTest {
  /*
  實現非文字檔案的複製
   */
  public static void main(String[] args) {
    BufferedInputStream bufferedInputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
      //1.造檔案
      File src = new File("基礎語法\\沙灘.jpg");
      //System.out.println(src.getAbsolutePath());
      File dest = new File("基礎語法\\沙灘3.jpg");
      //2.造流
      //2.1 節點流
      FileInputStream fileInputStream = new FileInputStream(src);
      FileOutputStream fileOutputStream = new FileOutputStream(dest);
      //2.2處理流(緩衝流)
      bufferedInputStream = new BufferedInputStream(fileInputStream);
      bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
      //3.複製的細節  讀取 寫入
      byte[] buffer = new byte[10];
      int  len;
      while ((len = bufferedInputStream.read(buffer)) != -1){
        bufferedOutputStream.write(buffer,0,len);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (bufferedOutputStream != null)
          bufferedOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (bufferedInputStream != null)
          bufferedInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    //4.資源關閉
    //要求:先關閉外層流 再關閉內層流
    //說明:在關閉外層流時,內層流會自動關閉  內層流的關閉可以省略
//    fileInputStream.close();
//    fileOutputStream.close();
  }
}
/**
 * 緩衝流的使用
 * 1.緩衝流
 * BufferedInputStream
 * BufferedOutputStream
 * BufferedReader
 * BufferedWriter
 *
 * 2。作用:提高讀取和寫入的速度
 *  提高速度的原因:內部提供了一個緩衝區
 *
 * 3. 處理流就是套接在已有的流的基礎上
 */
public class BufferedTest {
  /*
  實現文字檔案的複製
   */
  public static void main(String[] args) {
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    try {
      //建立檔案和對應的流
      bufferedReader = new BufferedReader(new FileReader(new File("基礎語法\\a.txt")));
      bufferedWriter = new BufferedWriter(new FileWriter(new File("基礎語法\\d.txt")));
      //讀寫檔案
//      //方式一
//      char[] buffer = new char[1024];
//      int len;
//      while ((len = bufferedReader.read(buffer)) != -1){
//        bufferedWriter.write(buffer,0,len);
      //方式二
      String data;
      while ((data = bufferedReader.readLine()) != null){
        //方法一
//        bufferedWriter.write(data+ "\n"); //data中不包含換行符  只有一行
        //方法二
        bufferedWriter.write(data);//data中不包含換行符
        bufferedWriter.newLine();  //提供換行操作
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //關閉資源
      try {
        if (bufferedReader!= null)
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (bufferedWriter != null)
        bufferedWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}