Java - DataOutputStream write(byte[] b, int off, int len)



描述

Java DataOutputStream write(byte[] b, int off, int len) 方法將從指定位元組陣列 b 的位置 off 開始的 len 個位元組寫入底層輸出流。

宣告

以下是 java.io.DataOutputStream.write(byte[] b, int off, int len) 方法的宣告:

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

引數

  • b − 源緩衝區。

  • off − 起始位置。

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

返回值

此方法不返回值。

異常

IOException − 如果發生 I/O 錯誤。

示例 1

以下示例演示了 Java DataOutputStream write(byte[] b, int off, int len) 方法的用法。我們建立了 ByteArrayOutputStream 和 DataOutputStream 引用。一個 byte[] buf 用一些位元組值初始化。建立了一個 ByteArrayOutputStream 物件。然後用之前建立的 ByteArrayOutputStream 物件初始化 DataOutputStream。然後將位元組陣列的一部分寫入 dataoutputstream。下一步,使用 flush() 方法重新整理流,並迭代 ByteArrayOutputStream 以列印寫入流的位元組。最後,我們關閉所有流。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      DataOutputStream dos = null;
      byte[] buf = {87,64,72,31,90};
      
      try {
         // create byte array output stream
         baos = new ByteArrayOutputStream();
         
         // create data output stream
         dos = new DataOutputStream(baos);
         
         // write to the stream from the source buffer
         dos.write(buf, 2, 3);
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
            System.out.println(b);
         }         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         
         // releases all system resources from the streams
         if(dos!=null)
            dos.close();
         if(baos!=null)
            baos.close();
      }
   }
}

輸出

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

72
31
90

示例 2

以下示例演示了 Java DataOutputStream write(byte[] b, int off, int len) 方法的用法。我們建立了 ByteArrayOutputStream 和 DataOutputStream 引用。一個 byte[] buf 用一些位元組值初始化。建立了一個 ByteArrayOutputStream 物件。然後用之前建立的 ByteArrayOutputStream 物件初始化 DataOutputStream。然後透過將索引設定為 0 並設定位元組陣列的長度,將完整的位元組陣列寫入 dataoutputstream。下一步,使用 flush() 方法重新整理流,並迭代 ByteArrayOutputStream 以列印寫入流的位元組。最後,我們關閉所有流。

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      DataOutputStream dos = null;
      byte[] buf = {87,64,72,31,90};
      
      try {
         // create byte array output stream
         baos = new ByteArrayOutputStream();
         
         // create data output stream
         dos = new DataOutputStream(baos);
         
         // write to the stream from the source buffer
         dos.write(buf, 0, buf.length);
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
            System.out.println(b);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(dos!=null)
            dos.close();
         if(baos!=null)
            baos.close();
      }
   }
}

輸出

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

87
64
72
31
90
java_dataoutputstream.htm
廣告
© . All rights reserved.