BufferedOutputStream write() method in Java with Examples

- The write(int) method of BufferedOutputStream class in Java is used to write the specified byte to the buffered output stream. The specified byte is passed as an integer to the write() method here. It is used to write one byte as a time to the BufferedOutputStream.
Syntax:
public void write(int b) throws IOExceptionOverrides: This method overrides the write(int) method of FilterOutputStream class.
Parameters: This method accepts b of integer type as a parameter which represents the byte to be written.
Return value: This method does not return any value.
Exception: This method throws IOException if an I/O error occurs.
Below program illustrates write(int) method in BufferedOutputStream class in IO package:
Program:
// Java program to illustrate// BufferedOutputStream write(int) methodimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args)throwsException{// Create byteArrayOutputStreamByteArrayOutputStream byteArrayOutStr=newByteArrayOutputStream();// Convert byteArrayOutputStream to// bufferedOutputStreamBufferedOutputStream buffOutputStr=newBufferedOutputStream(byteArrayOutStr);for(inti =65; i <70; i++) {// Writes to buffOutputStrbuffOutputStr.write(i);}// flush is called// to compel bytes to be// written out to buffOutputStrbuffOutputStr.flush();for(byteby : byteArrayOutStr.toByteArray()) {// Converts byte to charactercharch = (char)by;System.out.print(ch);}}}Output:ABCDE
- The write(byte[ ], int, int) method of BufferedOutputStream class in Java is used to write given length of bytes from the specified byte array starting at given offset to the buffered output stream.
Basically the write() method stores bytes from the given byte array into the buffer of a stream and flushes the buffer to the main output stream. If the length is equal to the buffer of the stream then write() method flushes the buffer and writes the bytes directly to the main output stream.Syntax:
public void write(byte[] b, int offset, int length) throws IOExceptionOverrides: This method overrides the write(byte[ ], int, int) method in FilterOutputStream class.
Parameters: This method accepts three parameters:
- b – It is of Byte type and represents the byte array.
- offset – It is of Integer type and represents the starting offset in the byte array.
- length – It is of Integer type and represents the number of bytes to be written.
Return value: This method does not return any value.
Exception: This method throws IOException if an I/O error occurs.
Below program illustrates write(byte[ ], int, int) method in BufferedOutputStream class in IO package:
Program:
// Java program to illustrate// BufferedOutputStream write(// byte[ ], int, int) methodimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args)throwsException{// Create byteArrayOutputStreamByteArrayOutputStream byteArrayOutStr=newByteArrayOutputStream();// Convert byteArrayOutputStream to// bufferedOutputStreamBufferedOutputStream buffOutputStr=newBufferedOutputStream(byteArrayOutStr);// Create byte arraybyteb[] = {71,69,69,75,83};// Call write(byte[ ], int, int)// method// It writes byte array to// buffOutputStrbuffOutputStr.write(b,0,5);// flush is called// to compel bytes to be// written out to buffOutputStrbuffOutputStr.flush();for(byteby : byteArrayOutStr.toByteArray()) {// Converts byte to charactercharch = (char)by;System.out.print(ch);}}}Output:GEEKS
References:



