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

Java IO 進階流全解析:內存流、打印流與標準流

     編輯:Java 綜合教學

超越文件與網絡:Java IO 中的特種流詳解

除了常見的文件流、緩衝流、轉換流,Java IO 體系還包含三類特殊流:內存操作流 (字節數組流)、打印流 (格式化輸出)、標準流 (系統級輸入輸出)。它們在數據交換、日志輸出、控制台交互等場景不可或缺。

一、內存操作流:字節數組與流的雙向轉換器

核心價值:將 byte[] 與 InputStream/OutputStream 互適配,實現純內存數據交換,避免磁盘/網絡 I/O 開銷。

1. ByteArrayInputStream:數組 → 輸入流

// 構造器
ByteArrayInputStream(byte[] buf)
ByteArrayInputStream(byte[] buf, int offset, int length)

// 典型用法
ByteArrayInputStream bis = new ByteArrayInputStream("abcd".getBytes());
int b;
while ((b = bis.read()) != -1) {
    System.out.println((char) b + " <=> " + b);
}
bis.close(); // 無實際效果,僅為規範

設計模式:適配器模式 —— 將 byte[] 適配為 InputStream 接口。

2. ByteArrayOutputStream:輸出流 → 數組

內部維護動態擴容的 byte[] 緩衝區,寫入數據自動增長。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(97); // 'a'
bos.write(new byte[]{98, 99, 100}); // 'b','c','d'

// 核心獲取數據方法
byte[] bytes = bos.toByteArray(); // 複製內部緩衝區數據
String str = bos.toString();      // 用平台默認字符集解碼
bos.close(); // 無效果,流仍可繼續使用

3. 雙向配合:內存流管道

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("data".getBytes());

ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
// 現在可以像讀文件一樣讀取內存數據
int b;
while ((b = bis.read()) != -1) System.out.print((char) b); // 輸出 data

應用場景:單元測試模擬流、序列化中間緩衝、內存壓縮/加密處理管道。

二、打印流:為人類可讀輸出而生

兩大實現:PrintStream (字節、繼承 FilterOutputStream) 與 PrintWriter (字符、繼承 Writer)。

四大特性

  1. 唯寫不讀:只有輸出方法,無輸入方法
  2. 萬能輸出:print/println 重載支持所有基本類型、對象、字符數組
  3. 自動刷新:構造器啟用 autoFlush=true 時,println、printf、format 觸發刷新
  4. 直接操作文件:提供 PrintStream(File)、PrintWriter(File) 構造器

啟用自動刷新

PrintWriter pw = new PrintWriter(new FileWriter("log.txt"), true); // 第二參數 true
PrintStream ps = new PrintStream(new FileOutputStream("log.txt"), true);

文件複製實戰

字節打印流版:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src.txt"));
PrintStream ps = new PrintStream(new FileOutputStream("dst.txt"), true);
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
    ps.println(new String(buf, 0, len)); // 每行自動刷新
}
ps.close(); bis.close();

字符打印流版:

BufferedReader br = new BufferedReader(new FileReader("src.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("dst.txt"), true);
String line;
while ((line = br.readLine()) != null) {
    pw.println(line);
}
pw.close(); br.close();

三、標準流:System.in/out/err 的真實身份

public final class System {
    public final static InputStream in = null;        // 標準輸入 (默認鍵盤)
    public final static PrintStream out = null;       // 標準輸出 (默認控制台)
    public final static PrintStream err = null;       // 標準錯誤輸出 (默認控制台)
}

本質揭秘

System.out.println("hello") 實際上是 IO 流操作:

PrintStream ps = System.out; // 獲取標準輸出流對象
ps.println("hello");         // 向流寫入數據,流將數據送達控制台設備

重定向實戰

// 將標準輸出重定向到文件
PrintStream fileOut = new PrintStream(new FileOutputStream("system.log"));
System.setOut(fileOut);
System.out.println("這行會寫入文件,而非控制台");

總結對比

流類型核心類數據方向數據類型典型場景
內存流ByteArrayInputStream/OutputStream雙向 (內存↔流)字節測試、序列化緩衝、內存管道
打印流PrintStream / PrintWriter僅輸出字節 / 字符日志、報表、控制台格式化輸出
標準流System.in/out/err輸入/輸出/錯誤字節/字符控制台交互、進程間通信、重定向

掌握這三類特種流,能讓你在數據處理、日志系統、測試模擬等場景中靈活構建高效的 IO 管道。

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