首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Java IO 體系全景圖:字節流、字符流、轉換流與實戰代碼模板大全

     編輯:Java 綜合教學

前言:理清 40+ 類的繼承體系,告別「用時混亂、過後遺忘」

Java IO 涉及 40 多個類,初學者極易混淆。本文以「字節 vs 字符」「節點 vs 處理」「轉換橋樑」三條主線,梳理完整體系結構,並提供高頻實戰代碼模板(InputStream↔String、文件讀寫、緩衝行讀),可直接複製落地。

一、 核心抽象基類:四大家族

維度 輸入流 (讀) 輸出流 (寫)
字節流 (二進制,萬能) InputStream (抽象基類)
核心方法:read(), read(byte[]), close()
OutputStream (抽象基類)
核心方法:write(int), write(byte[]), flush(), close()
字符流 (文本,便捷) Reader (抽象基類)
核心方法:read(), read(char[]), close()
Writer (抽象基類)
核心方法:write(int), write(char[]), flush(), close()

選型原則:處理文本優先字符流(自動處理編碼);圖片/視頻/二進制文件必須字節流;不確定時用字節流保底。

二、 體系全景圖(文字版)

InputStream (字節輸入)
├── FileInputStream (文件源)
├── ByteArrayInputStream (內存字節數組源)
├── FilterInputStream (裝飾者基類)
│   ├── BufferedInputStream (緩衝,提升性能)
│   ├── DataInputStream (基本類型讀取)
│   └── ObjectInputStream (對象反序列化)
├── PipedInputStream (管道通信)
└── SequenceInputStream (合併多輸入流)

OutputStream (字節輸出) ... 對應結構

Reader (字符輸入)
├── InputStreamReader (字節→字符轉換橋樑,指定編碼)
│   └── BufferedReader (緩衝行讀 readLine())
├── FileReader (便捷文件字符讀,默認編碼)
├── CharArrayReader (字符數組源)
├── PipedReader (管道)
└── StringReader (字符串源)

Writer (字符輸出) ... 對應結構

三、 轉換流:字節與字符的橋樑

核心類:

  • InputStreamReader:字節輸入流 → 字符輸入流(解碼,指定 charset)
  • OutputStreamWriter:字節輸出流 → 字符輸出流(編碼,指定 charset)

經典組合:高效讀取文本文件(按行)

// 模板:BufferedReader 包裝 InputStreamReader,指定 UTF-8
try (BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream("data.txt"), StandardCharsets.UTF_8))) {
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line); // 業務處理
    }
}

四、 實戰代碼模板庫(可直接復用)

4.1 InputStream → String (內存流法)

public static String inputStreamToString(InputStream is, String charset) throws IOException {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        return baos.toString(charset);
    }
}

4.2 InputStream → StringBuffer (字符數組法,適合大文件流式處理)

public static String inputStreamToStringBuffer(InputStream is, String charset) throws IOException {
    StringBuffer sb = new StringBuffer();
    try (InputStreamReader isr = new InputStreamReader(is, charset);
         BufferedReader br = new BufferedReader(isr)) {
        char[] buf = new char[1024];
        int len;
        while ((len = br.read(buf)) != -1) {
            sb.append(buf, 0, len);
        }
    }
    return sb.toString();
}

4.3 文件讀寫完整示例(帶緩衝、異常處理、資源自動關閉)

public class FileIOExample {
    public static void main(String[] args) {
        String src = "src.txt", dest = "dest.txt";
        // 字節流複製(圖片/二進制通用)
        copyFileByStream(src, dest);
        // 字符流複製(文本文件)
        copyFileByReader(src + ".txt", dest + ".txt");
    }

    // 字節流複製模板
    static void copyFileByStream(String src, String dest) {
        try (InputStream in = new BufferedInputStream(new FileInputStream(src));
             OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) {
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) != -1) out.write(buf, 0, len);
            out.flush();
        } catch (IOException e) { throw new UncheckedIOException(e); }
    }

    // 字符流複製模板
    static void copyFileByReader(String src, String dest) {
        try (Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(src), StandardCharsets.UTF_8));
             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest), StandardCharsets.UTF_8))) {
            char[] buf = new char[8192];
            int len;
            while ((len = in.read(buf)) != -1) out.write(buf, 0, len);
            out.flush();
        } catch (IOException e) { throw new UncheckedIOException(e); }
    }
}

五、 避坑指南與最佳實踐

  1. 必須關閉流:JDK 7+ try-with-resources 強制自動關閉,拒絕手動 finally 易漏關。
  2. 指定編碼:構造 InputStreamReader/OutputStreamWriter 時顯式傳 StandardCharsets.UTF_8,避免跨平台亂碼。
  3. 緩衝是銀彈:無論讀寫,外層必包 BufferedInputStream/OutputStream 或 BufferedReader/Writer,減少系統調用次數,性能提升數量級。
  4. 大文件分塊:避免一次性 readAllBytes() 導致 OOM,採用固定緩衝區循環讀寫。
  5. NIO.2 升級:新項目優先考慮 Files.newBufferedReader()、Files.copy()、FileChannel 等現代 API。

結語

Java IO 體系雖大,掌握「四大基類 → 裝飾者模式組合 → 轉換流橋樑 → 緩衝優化」這條主線,配合 try-with-resources 模板化編碼,即可應對 95% 以上業務場景。建議收藏代碼模板,用時拿來即用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved