- 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 byteArrayOutputStream       ÂByteArrayOutputStream byteArrayOutStr           Â=newByteArrayOutputStream();       Â// Convert byteArrayOutputStream to       Â// bufferedOutputStream       ÂBufferedOutputStream buffOutputStr           Â=newBufferedOutputStream(               ÂbyteArrayOutStr);       Âfor(inti =65; i <70; i++) {           Â// Writes to buffOutputStr           ÂbuffOutputStr.write(i);       Â}       Â// flush is called       Â// to compel bytes to be       Â// written out to buffOutputStr       ÂbuffOutputStr.flush();       Âfor(           Âbyteby : byteArrayOutStr.toByteArray()) {           Â// Converts byte to character           Âcharch = (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 byteArrayOutputStream       ÂByteArrayOutputStream byteArrayOutStr           Â=newByteArrayOutputStream();       Â// Convert byteArrayOutputStream to       Â// bufferedOutputStream       ÂBufferedOutputStream buffOutputStr           Â=newBufferedOutputStream(               ÂbyteArrayOutStr);       Â// Create byte array       Âbyteb[] = {71,69,69,75,83};       Â// Call write(byte[ ], int, int)       Â// method       Â// It writes byte array to       Â// buffOutputStr       ÂbuffOutputStr.write(b,0,5);       Â// flush is called       Â// to compel bytes to be       Â// written out to buffOutputStr       ÂbuffOutputStr.flush();       Âfor(           Âbyteby : byteArrayOutStr.toByteArray()) {           Â// Converts byte to character           Âcharch = (char)by;           ÂSystem.out.print(ch);       Â}   Â}}Output:GEEKS
References:
