Java - BufferedOutputStream write() method



Description

The Java BufferedOutputStream write() method writes byte to the output stream. write() method writes the lowest byte (8 bits) of the given integer i to the output stream. The method writes a single byte, so only the least significant byte of the integer is considered (e.g., i & 0xFF).

Declaration

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

public void write(int b)

Parameters

b − byte to be written to the output stream.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Writing an integer to OutputStream

The following example shows the usage of Java BufferedOutputStream write(int b) method.

BufferedOutputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      ByteArrayOutputStream baos = null;
      BufferedOutputStream bos = null;
		
      try {
         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();

         // create new BufferedOutputStream with baos
         bos = new BufferedOutputStream(baos);
			
         // assign integer
         int b = 87;

         // write to stream
         bos.write(b);

         // force the byte to be written to baos
         bos.flush();
			
         // convert ByteArrayOutputStream to bytes
         byte[] bytes = baos.toByteArray();	

         // prints the byte
         System.out.println(bytes[0]);
         
      } catch(IOException e) {
         // if I/O error occurs.
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(baos!=null)
            baos.close();
         if(bos!=null)
            bos.close();
      }
   }
}

Output

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

87

Example - Writing Single Characters to a File

The following example shows the usage of Java BufferedOutputStream write(int b) method.

BufferedOutputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedOutputStreamDemo {
   public static void main(String[] args) {
      try (BufferedOutputStream bos = new BufferedOutputStream(
         new FileOutputStream("example.txt"))) {
         // Write individual characters as integers
         bos.write('H'); // Writes the character 'H'
         bos.write('i'); // Writes the character 'i'
         bos.write('!'); // Writes the character '!'

         // Ensure data is flushed to the file
         bos.flush();
         System.out.println("Characters written to example.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Characters written to example.txt

Explanation

  • Input− Characters 'H', 'i', and '!' are written to the file using their ASCII integer values.

    • 'H' (ASCII value 72), 'i' (ASCII value 105), and '!' (ASCII value 33) are written as bytes.

  • Flushing

    • The flush() method ensures that the buffered data is written to the file immediately.

  • Output

    • The file example.txt will contain−

    • Hi!

Example - Writing Byte Values Beyond ASCII Range

The following example shows the usage of Java BufferedOutputStream write(int b) method.

BufferedOutputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedOutputStreamDemo {
   public static void main(String[] args) {
      try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         BufferedOutputStream bos = new BufferedOutputStream(baos)) {

         // Write integer values as bytes (only the lowest byte is written)
         bos.write(255); // 255 (0xFF) -> -1 in signed byte representation
         bos.write(128); // 128 (0x80) -> -128 in signed byte representation
         bos.write(65);  // 65 (0x41) -> ASCII 'A'

         // Flush the buffer to the ByteArrayOutputStream
         bos.flush();

         // Print the raw byte data in ByteArrayOutputStream
         byte[] result = baos.toByteArray();
         System.out.print("Bytes written: ");
         for (byte b : result) {
            System.out.print(b + " ");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Bytes written: -1 -128 65

Explanation

  • Input

    • The values 255, 128, and 65 are written to the stream.

    • Only the lowest byte of each integer is considered−

      • 255 (0xFF) writes -1 (in signed byte representation).

      • 128 (0x80) writes -128 (in signed byte representation).

      • 65 (0x41) writes ASCII 'A'.

  • Flushing

    • The flush() ensures the buffered bytes are written to the underlying ByteArrayOutputStream.

Key Points

  • Behavior

    • The write(int i) method writes the least significant byte (8 bits) of the integer i.

    • Values beyond 255 are truncated, and only the lowest byte is written.

  • Flushing

    • The flush() method ensures buffered data is written to the stream immediately.

  • Use Cases

    • Writing single bytes or characters.

    • Handling raw byte-level data where only specific parts of integers need to be written.

java_io_bufferedoutputstream.htm
Advertisements