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

Java IO 流體系全景解析:字節流、字符流、緩衝流與掃描器實戰

     編輯:Java 相關知識

流的分類體系

維度分類基類/接口典型實現處理單位
按方向輸入流InputStream / ReaderFileInputStream, FileReader數據流向程序
輸出流OutputStream / WriterFileOutputStream, FileWriter
按數據單位字節流InputStream / OutputStreamFileInputStream, ByteArrayOutputStream字節 (byte/8bit)
字符流Reader / WriterFileReader, StringWriter字符 (char/16bit)
按功能節點流/處理流-BufferedInputStream, DataOutputStream裝飾器模式增強

核心代碼實戰

1. 字節流複製文件 (圖片/視頻等二進制)

public static void copyByByteStream(File src, File dest) throws IOException {
    try (FileInputStream in = new FileInputStream(src);
         FileOutputStream out = new FileOutputStream(dest)) {
        byte[] buffer = new byte[8192];
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    }
}

2. 字符流處理文本文件

public static void writeText(File file, String content, boolean append) throws IOException {
    try (FileWriter writer = new FileWriter(file, append)) {
        writer.write(content);
    }
}

public static String readText(File file) throws IOException {
    StringBuilder sb = new StringBuilder();
    try (FileReader reader = new FileReader(file)) {
        char[] buffer = new char[1024];
        int len;
        while ((len = reader.read(buffer)) != -1) {
            sb.append(buffer, 0, len);
        }
    }
    return sb.toString();
}

3. 緩衝流大幅提升性能 (必用)

// 寫入
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
    bw.write("高效寫入");
    bw.newLine(); // 跨平台換行
}

// 讀取逐行
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
        process(line);
    }
}

4. Scanner 靈活解析 (推薦文本讀取)

public static void parseWithScanner(File file) throws FileNotFoundException {
    try (Scanner scanner = new Scanner(file, StandardCharsets.UTF_8.name())) {
        // 按行讀取
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
        }
        // 按正則分隔符解析
        scanner.useDelimiter("\\s*,\\s*");
        while (scanner.hasNext()) {
            String token = scanner.next();
        }
    }
}

轉換流:字節與字符橋梁

// 字節流 → 字符流 (指定編碼)
Reader reader = new InputStreamReader(inputStream, "UTF-8");
Writer writer = new OutputStreamWriter(outputStream, "GBK");

// 實際應用:網絡流轉字符處理
try (BufferedReader br = new BufferedReader(
        new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
    String response = br.readLine();
}

特殊流應用場景

流類型用途示例
DataInputStream/DataOutputStream基本類型序列化writeInt(), readDouble()
ObjectInputStream/ObjectOutputStream對象序列化實現 Serializable 接口
PrintWriter/PrintStream格式化輸出printf(), println()
ByteArrayInputStream/ByteArrayOutputStream內存緩衝操作toByteArray(), toString()
PipedInputStream/PipedOutputStream線程間管道通信生產者-消費者模型
SequenceInputStream合併多個輸入流合併多個文件

NIO.2 現代文件操作 (JDK 7+)

// 文件讀寫
List<String> lines = Files.readAllLines(Paths.get("file.txt"), StandardCharsets.UTF_8);
Files.write(Paths.get("out.txt"), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE);

// 大文件流式處理
try (Stream<String> stream = Files.lines(Paths.get("big.log"))) {
    stream.filter(l -> l.contains("ERROR")).forEach(System.out::println);
}

// 文件屬性操作
Path path = Paths.get("data.txt");
Files.getLastModifiedTime(path);
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rw-r--r--"));

最佳實踐總結

  1. 二進制文件:字節流 + 緩衝 (BufferedInputStream/OutputStream)
  2. 文本文件:字符流 + 緩衝 (BufferedReader/Writer) 或 Scanner
  3. 必須指定編碼:構造 InputStreamReader/OutputStreamWriter 時顯式傳 charset
  4. try-with-resources:自動關閉,防止洩漏
  5. 大文件流式處理:避免一次性讀入內存,使用 Files.lines() 或 BufferedReader 循環
  6. 性能關鍵:緩衝區大小 8KB-64KB,減少系統調用

總結

Java IO 體系雖龐大,但掌握「字節/字符、輸入/輸出、節點/處理」三個維度分類,熟練使用緩衝流、轉換流、Scanner、NIO.2,即可應對 95% 文件與網絡數據處理場景。現代開發優先選擇 NIO.2 API,代碼更簡潔、功能更強大、異常處理更規範。

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