Java.io.ByteArrayOutputStream.write() Method

Advertisements


Description

The java.io.ByteArrayOutputStream.write(int b) method writes the specified byte to this stream.

Declaration

Following is the declaration for java.io.ByteArrayOutputStream.write(int b) method:

public void write(int b)

Parameters

  • b -- The specified byte as integer in the range of 0-255

Return Value

This method doesn't return any value.

Exception

  • NA

Example

The following example shows the usage of java.io.ByteArrayOutputStream.write(int b) method.

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      
      String str = "";
      ByteArrayOutputStream baos = null;
      
      try{

         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();
      
         // write byte array to the output stream
         baos.write(55);
         
         // converts the byte to the default charset value
         str = baos.toString();
         
         // prints the string
         System.out.println(str);
         
      }catch(Exception e){

         // if I/O error occurs
         e.printStackTrace();
      }finally{
         if(baos!=null)
            baos.close();
      }   
   }
}

Let us compile and run the above program, this will produce the following result:

7



Advertisements
Advertisements