1. 程式人生 > 其它 >【IO程式設計】4.節點流和處理流

【IO程式設計】4.節點流和處理流

1. 基本介紹

  1. 節點流是從一個特定的資料來源讀寫資料,如FileReader、FileInputStream
  2. 處理流也叫包裝流,對已存在的流(節點流或處理流)進行包裝,提供更強大的讀寫功能,如:BuffededReader、BufferedInputStream

2. 節點流和處理流的劃分

3. 節點流和處理流的異同

  1. 節點流是底層流,直接跟資料來源連線。
  2. 處理流使用了裝飾器設計模式,對節點流進行包裝。
  3. 處理流包裝節點流,可以消除不同節點流的實現差異,提供統一的介面完成輸入輸出。

4. 處理流的優勢

  1. 提高效能:增加了緩衝的方式提高輸入輸出的效率。
  2. 操作便捷:提供多個方法來一次輸入輸出大批量資料,使用靈活。

5. BufferedReader介紹

  • 構造方法
    • BufferedReader(Reader in, int sz)
      • in: 節點流或處理流的字元輸入流物件
      • sz: 快取大小
    • BufferedReader(Reader in)
      • 使用預設快取大小8192
  • 成員方法
    • String readLine()
      • 讀取一行,忽略最後的"\n"
    • void close()
      • 呼叫處理流的close()方法時會呼叫位元組流的close()方法

使用BufferedReader讀取文字檔案,程式碼如下:

@Test
public void readFile() {
    BufferedReader br = null;
    String line;
    try {
        br = new BufferedReader(new FileReader("e:\\test.txt"));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

6. BufferedWriter介紹

  • 構造方法

    • BufferedWriter(Writer out, int sz)
      • out: 節點流或處理流字元輸出流物件
      • sz: 快取大小
    • BufferedWriter(Writer out)
      • 使用預設快取大小8192
  • 成員方法

    • void write(String str)
    • void write(String str, int off, int len)
      • 將字串寫入檔案
    • void newLine()
      • 插入一個和系統相關的換行符
    • void close()
      • 呼叫處理流的close()方法時會呼叫位元組流的close()方法
@Test
public void writeFile() {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter("e:\\test.txt"));
        bw.write("hello, world");
        bw.newLine();
        bw.write("hello, java");
        bw.newLine();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

7. 使用BufferedReader和BufferedWriter實現文字檔案拷貝

此方法只適用於文字檔案拷貝,用於二進位制檔案拷貝會損壞檔案。程式碼如下:

public boolean copyFile(String sourceFilePath, String destFilePath) {
    boolean success = false;
    BufferedReader br = null;
    BufferedWriter bw = null;
    String line;
    try {
        br = new BufferedReader(new FileReader(sourceFilePath));
        bw = new BufferedWriter(new FileWriter(destFilePath));
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
        }
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return success;
}

8. BufferedInputStream介紹

  • 構造方法
    • BufferedInputStream(InputStream in, int size)
      • in: 節點流或處理流的位元組輸入流物件
      • size: 快取大小
    • BufferedInputStream(InputStream in)
      • 使用預設快取大小8192
  • 成員方法
    • int read(byte[] buf)
      • 批量讀取位元組到陣列中,返回讀取到的位元組數,如果到檔案末尾返回-1
    • int read()
      • 讀取一個位元組
    • void close()
      • 呼叫處理流的close()方法時會呼叫位元組流的close()方法

9. BufferedOutputStream介紹

  • 構造方法
    • BufferedOutputStream(OutputStream out, int size)
      • out: 節點流或處理流的位元組輸出流物件
      • size: 快取大小
    • BufferedOutputStream(OutputStream out)
      • 使用預設快取大小8192
  • 成員方法
    • void write(int b)
      • 寫入一個位元組
    • void write(byte[] b, int off, int len)
      • 寫入指定陣列的指定部分,以索引off開始,長度為len
    • void close()
      • 呼叫處理流的close()方法時會呼叫位元組流的close()方法

10. 使用BufferedInputStream和BufferedOutputStream實現檔案拷貝

實現思路與BufferedReader和BufferedWriter基本一致,程式碼如下:

public boolean copyFile(String sourceFilePath, String destFilePath) {
    boolean success = false;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    byte[] buffer = new byte[8192];
    int readLength = 0;
    try {
        bis = new BufferedInputStream(new FileInputStream(sourceFilePath));
        bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
        while ((readLength = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, readLength);
        }
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return success;
}