Princeton Algorithms 4th Edition 配套 stdlib.jar 提供簡潔的 I/O 抽象:In/Out(檔案/網路/字串)與 StdIn/StdOut(標準流)。本文詳解配置與核心 API 用法。
stdlib.jareclipse/dropins 目錄重啟| 類別 | 資料來源/目的地 | 典型用途 |
|---|---|---|
In | 檔案、URL、字串、標準輸入 | 批次讀取檔案/網路資源 |
Out | 檔案、標準輸出、字串 | 格式化輸出至檔案/控制台 |
StdIn | 標準輸入 | 互動式控制台讀取 |
StdOut | 標準輸出 | 控制台格式化輸出 |
public class StdLibDemo {
public static void main(String[] args) {
// In & Out:檔案處理
Out out = new Out(System.out);
out.println("讀取整數檔案: ");
In in = new In(args[0]); // 命令列參數傳檔案路徑
int[] arr = in.readAllInts();
for (int i : arr) out.print(i + " ");
out.println("end");
// StdIn & StdOut:控制台互動
StdOut.println("請輸入整數陣列 (Ctrl+Z 結束): ");
while (!StdIn.isEmpty()) {
int val = StdIn.readInt();
StdOut.print(val + " ");
}
StdOut.println("end");
}
}
readAllInts()、readAllDoubles()、readAllStrings()、readAllLines()readInt()、readDouble()、readString()、readLine()、readChar()isEmpty()、hasNextLine()print()、println()、printf()new Out() (stdout)、new Out(file)、new Out(url)StdIn.isEmpty() 檢測 EOF (Windows: Ctrl+Z, Unix: Ctrl+D)讀取整數檔案: 1 2 3 4 5 6 7 8 9 10 end 請輸入整數陣列 (Ctrl+Z 結束): 11 12 13 14 15 11 12 13 14 15 end