Java - DataOutputStream writeByte(int value)



Description

The Java DataOutputStream writeByte(int v) method writes a byte to the underlying stream as a 1-byte value. The counter is incremented by 1 on successful execution of this method.

Declaration

Following is the declaration for java.io.DataOutputStream.writeByte(int v) method −

public final void writeByte(int v)

Parameters

v − A byte value to be written to the stream.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example 1

The following example shows the usage of Java DataOutputStream writeByte(int b) method. We've created ByteArrayOutputStream and DataOutputStream reference. A int[] buf is initialized with some int values. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. buf array is iterated to write int to stream using writeByte() method. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream as characters. Finally we're closing all the streams.

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.writeByte(i);
         }
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
         
            // print value
            System.out.print(b + " ");
         }
         
      } 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();
      }
   }
}

Output

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

65 66 67 68 69 70 71 

Example 2

The following example shows the usage of Java DataOutputStream writeByte(int b) method. We've created ByteArrayOutputStream and DataOutputStream reference. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. We're writing few values to writeByte() method. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream as characters. Finally we're closing all the streams.

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;
           
      try {
         // create byte array output stream
         baos = new ByteArrayOutputStream();
         
         // create data output stream
         dos = new DataOutputStream(baos);
         
         // write some values to stream
         dos.writeByte(65);
		 dos.writeByte(66);         
         
         // flushes bytes to underlying output stream
         dos.flush();
   
         // for each byte in the baos buffer content
         for(byte b:baos.toByteArray()) {
            
            // print value
            System.out.print(b + " ");
         }
         
      } 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();
      }
   }
}

Output

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

65 66 
java_dataoutputstream.htm
Advertisements