當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.DataOutputStream.write()用法及代碼示例


描述

這個java.io.BufferedInputStream.write(byte[] b, int off, int len)方法寫len來自指定字節數組的字節b從位置開始off到底層輸出流。

聲明

以下是聲明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 錯誤。

示例

下麵的例子展示了 java.io.DataOutputStream.write(byte[] b, int off, int len) 方法的用法。

package com.tutorialspoint;

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

相關用法


注:本文由純淨天空篩選整理自 Java.io.DataOutputStream.write() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。