Open In App

BufferedOutputStream write() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report
  1. 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 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
    
  2. 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 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:



Last Updated : 05 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads