BufferedOutputStream write() method in Java with Examples Last Updated : 05 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 IOException Overrides: 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 // Java program to illustrate // BufferedOutputStream write(int) method import java.io.*; public class GFG { public static void main( String[] args) throws Exception { // Create byteArrayOutputStream ByteArrayOutputStream byteArrayOutStr = new ByteArrayOutputStream(); // Convert byteArrayOutputStream to // bufferedOutputStream BufferedOutputStream buffOutputStr = new BufferedOutputStream( byteArrayOutStr); for (int i = 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 ( byte by : byteArrayOutStr.toByteArray()) { // Converts byte to character char ch = (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 IOException Overrides: 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 // Java program to illustrate // BufferedOutputStream write( // byte[ ], int, int) method import java.io.*; public class GFG { public static void main( String[] args) throws Exception { // Create byteArrayOutputStream ByteArrayOutputStream byteArrayOutStr = new ByteArrayOutputStream(); // Convert byteArrayOutputStream to // bufferedOutputStream BufferedOutputStream buffOutputStr = new BufferedOutputStream( byteArrayOutStr); // Create byte array byte b[] = { 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 ( byte by : byteArrayOutStr.toByteArray()) { // Converts byte to character char ch = (char)by; System.out.print(ch); } } } Output: GEEKS References: https://docs.oracle.com/javase/10/docs/api/java/io/BufferedOutputStream.html#write(int) https://docs.oracle.com/javase/10/docs/api/java/io/BufferedOutputStream.html#write(byte%5B%5D, int, int) Comment More infoAdvertise with us Next Article BufferedOutputStream write() method in Java with Examples P pp_pankaj Follow Improve Article Tags : Java Java-Functions Java-IO package Practice Tags : Java Similar Reads ByteArrayOutputStream write() method in Java with Examples The write() method of ByteArrayOutputStream class in Java is used in two ways: 1. The write(int) method of ByteArrayOutputStream class in Java is used to write the specified byte to the ByteArrayOutputStream. This specified byte is passed as integer type parameter in this write() method. This write( 2 min read BufferedInputStream reset() method in Java with Examples The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class. General Contract: There are two cases: If mark() and reset() are suppor 3 min read BufferedInputStream read() method in Java with Examples read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time. Syntax: public int read() Overrides: It overrides read() me 3 min read BufferedInputStream skip(long) method in Java with Examples The skip(long) method of BufferedInputStream class in Java is used to skip n bytes of data from the buffered input stream. The number of bytes skipped is stored and returned as long type. The termination condition involves either of the two: Either reading into a byte array until n-bytes are covered 2 min read Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark 3 min read Like