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

Java 輸出流體系詳解:PrintStream、DataOutputStream 與 RandomAccessFile

     編輯:Java 相關知識

兩大分類

  • 格式化輸出:PrintStream 面向人類閱讀,支援 print/println 所有基本型別與物件。
  • 數據存儲:DataOutputStream 攜帶型別資訊,配合 DataInputStream 可移植讀寫。

關鍵類

類別用途典型構造
PrintStream控制台、日誌、文字檔new PrintStream(new BufferedOutputStream(new FileOutputStream(f)))
DataOutputStream二進制數據交換new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)))
RandomAccessFile隨機讀寫檔案new RandomAccessFile(f, "rw")

RandomAccessFile 特性

  • 實作 DataInput/DataOutput,但不屬於 Stream 體系。
  • seek(pos) 移動指標,getFilePointer() 取得位置,length() 檔案長度。
  • 模式 "r" 唯讀、"rw" 讀寫。

數據寫入範例

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
out.writeBytes("Pi: \n");
out.writeDouble(3.14159);
out.close();
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved