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

Java 輸入流全景指南:緩衝、數據解析、行號與推回

     編輯:Java 相關知識

核心流類別

類別功能典型構造
DataInputStream讀取基本型別、字串new DataInputStream(new BufferedInputStream(new FileInputStream(f)))
BufferedInputStream緩衝讀取提升效能new BufferedInputStream(new FileInputStream(f), 8192)
LineNumberInputStream追蹤行號new LineNumberInputStream(new FileInputStream(f))
PushbackInputStream單字節推回(編譯器掃描用)new PushbackInputStream(new FileInputStream(f))

常見讀取模式

逐行讀取

DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
String line;
while ((line = in.readLine()) != null) { ... }

內存字串流

StringBufferInputStream mem = new StringBufferInputStream(str);
DataInputStream in = new DataInputStream(mem);
while (in.available() > 0) System.out.print((char) in.readByte());

行號追蹤

LineNumberInputStream lnis = new LineNumberInputStream(new StringBufferInputStream(s2));
DataInputStream in = new DataInputStream(lnis);
while ((line = in.readLine()) != null) {
    System.out.println(lnis.getLineNumber() + ": " + line);
}

EOF 檢測

readLine() 返回 null、read() 返回 -1、available() == 0、捕獲 EOFException 四種方式視場景選擇。

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