Java.io.PrintStream.write() 方法



描述

java.io.PrintStream.write() 方法將指定位元組陣列中從偏移量 off 開始的 len 個位元組寫入此流。如果啟用了自動重新整理,則將呼叫 flush 方法。

請注意,位元組將按原樣寫入;要寫入根據平臺的預設字元編碼進行轉換的字元,請使用 print(char) 或 println(char) 方法。

宣告

以下是 java.io.PrintStream.write() 方法的宣告。

public void write(byte[] buf,int off,int len)

引數

  • buf − 位元組陣列。

  • off − 開始獲取位元組的偏移量。

  • len − 要寫入的位元組數。

返回值

此方法不返回值。

異常

示例

以下示例顯示了 java.io.PrintStream.write() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class PrintStreamDemo {
   public static void main(String[] args) {
      byte c[] = {70, 71, 72, 73, 74, 75, 76};

      // create printstream object
      PrintStream ps = new PrintStream(System.out);

      // write bytes 1-3
      ps.write(c, 1, 3);

      // flush the stream
      ps.flush();
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

GHI
java_io_printstream.htm
廣告