java.io.BufferedOutputStream write(byte[] b, int off, int len)

Description

On this document we will be showing a java example on how to use the write(byte[] b, int off, int len) method of BufferedOutputStream Class. This method writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Ordinarily this method stores bytes from the given array into this stream’s buffer, flushing the buffer to the underlying output stream as needed. If the requested length is at least as large as this stream’s buffer, however, then this method will flush the buffer and write the bytes directly to the underlying output stream. Thus redundant BufferedOutputStreams will not copy data unnecessarily.

Override by:

write in class FilterOutputStream

Throws:

  • IOException – If an I/O error occurs

Method Syntax

public void write(byte[] b, int off, int len)
throws IOException

Method Argument

Data Type Parameter Description
byte[] b the data
int off the start offset in the data.
int len the number of bytes to write.

Method Returns

This method returns void.

Compatibility

Requires Java 1.0 and up

Java BufferedOutputStream write(byte[] b, int off, int len) Example

Below is a java code demonstrates the use of write(byte[] b, int off, int len) method of BufferedOutputStream class. The example presented might be simple however it shows the behaviour of the write(byte[] b, int off, int len).

package com.javatutorialhq.java.examples;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;


/*
 * This example source code demonstrates the use of  
 * 	write(byte[] b, int off, int len) method of BufferedOutputStream class
 */

public class BufferedOutputStreamWriteByteOffsetLengthExample {

	public static void main(String[] args) {	
		
		
		try {
			// initialize File object
			File f = new File("C:javatutorialhqoutputtest_out.txt");
			
			// instantiate a new FileOutputStream
			FileOutputStream fos = new FileOutputStream(f);
			
			// using the above FileOutputStream
			// instantiate a BufferedOutputStream object
			BufferedOutputStream buffOut = new BufferedOutputStream(fos);
			
			// initialize variables
			String writeString = "This is a test string";
			
			// convert above String to character array
			char[] value = writeString.toCharArray();
			
			// instantiate a by
			byte[] byteArray = new byte[writeString.length()];
			
			int i = 0;
			// covert char array to byte array
			for(char c:value){
				byteArray[i]=(byte)c;
				i++;
			}
			
			// write the contents of the byte array
			buffOut.write(byteArray, 0, byteArray.length);
			
			// close the BufferedOutputStream
			buffOut.flush();
			buffOut.close();
			
			// read the output file
			System.out.println("*****Contents of output file*****");
			Scanner s = new Scanner(f);
			while(s.hasNextLine()){
				System.out.println(s.nextLine());
			}
			s.close();
			
		} catch (FileNotFoundException e) {
			System.out.println("File path not found");
		} catch (IOException e) {
			System.out.println("IOException occured");
		}	

	}

}

Sample Output

Below is the sample output when you run the above example.

java BufferedOutputStream write(byte[] b, int off, int len) example output