void write(int b)


描述

所述java.io.BufferedInputStream.write(INT B)方法指定的源字节写入到底层输出流。写入的计数器在成功调用时递增1。

声明

以下是java.io.DataOutputStream.write(int b)方法的声明

public void write(int b)

参数

b - 源字节为整数。

返回值

此方法不返回任何值。

异常

IOException - 如果发生I / O错误。

实例

以下示例显示了java.io.DataOutputStream.write(int b)方法的用法。

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;
      int[] buf = {65, 66, 67, 68, 69, 70, 71};

      try {
         // create byte array output stream
         baos = new ByteArrayOutputStream();

         // create data output stream
         dos = new DataOutputStream(baos);

         // write to the stream from integer array
         for(int i: buf) {
            dos.write(i);
         }

         // flushes bytes to underlying output stream
         dos.flush();

         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {

            // convert byte to char
            char c = (char)b;

            // print character
            System.out.print(c);
         }

      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(baos!=null)
            baos.close();
         if(dos!=null)
            dos.close();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果

ABCDEFG