Open In App

BufferedWriter write() method in Java with Examples

The write() method in BufferedWriter class in Java is used in three ways:

1. The write(int) method of BufferedWriter class in Java is used to write a single character at a time in the buffer writer stream.



Syntax:

public void write(int ch)
            throws IOException

Overrides: This method overrides the write() method of Writer class.



Parameters: This method accept single parameter ch which represents the character that is to be written.

Return value: This method does not return any value.

Exceptions: This method throws IOException if an I/O error occurs.

Below program illustrates write(int) method in BufferedWriter class in IO package:

Program:




// Java program to illustrate
// BufferedWriter write(int) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "G" to buffer writer
        buffWriter.write(71);
  
        // Write "E" to buffer writer
        buffWriter.write(69);
  
        // Write "E" to buffer writer
        buffWriter.write(69);
  
        // Write "K" to buffer writer
        buffWriter.write(75);
  
        // Write "S" to buffer writer
        buffWriter.write(83);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}

Output:
GEEKS

2. The write(String s, int off, int len) method of BufferedWriter class in Java is used to write a part of a string passed as parameter in the buffer writer stream.

Syntax:

public void write(String s,
                  int off,
                  int len)
           throws IOException

Overrides: This method overrides the write() method of Writer class.

Parameters: This method accept three parameters:

Return value: This method does not return any value.

Exceptions:

Note: This method should throw an IndexOutOfBoundsException if the length of the portion of the string (len) is negative or index is negative but this method does not throw such an exception in these cases. It writes no characters instead of throwing an exception in such cases.

Below program illustrates write(String) method in BufferedWriter class in IO package:

Program:




// Java program to illustrate
// BufferedWriter write(String) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "GEEKS" to buffer writer
        buffWriter.write(
            "GEEKSFORGEEKS", 0, 5);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}

Output:
GEEKS

3. The write(char[ ] cbuf, int off, int len) method of BufferedWriter class in Java is used to write a part of an array of characters passed as parameter in the buffer writer stream. This method generally stores the characters from the array into the stream and flushes the buffer to the mainstream. It can directly use the mainstream when the length of the buffer is equal to length of the array.

Syntax:

public void write(char[ ] cbuf,
                  int off,
                  int len)
           throws IOException

Specified By: This method is specified by the write() method of Writer class.

Parameters: This method accept three parameters:

Return value: This method does not return any value.

Exceptions:

Below program illustrates write(char[ ]) method in BufferedWriter class in IO package:

Program:




// Java program to illustrate
// BufferedWriter write(char[ ]) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
        // Create character array
        char cbuf[] = { 'G', 'E', 'E', 'K',
                        'S', 'F', 'O', 'R' };
  
        // Write "GEEKS" to buffer writer
        buffWriter.write(cbuf, 0, 5);
  
        // Write "FOR" to buffer writer
        buffWriter.write(cbuf, 5, 3);
  
        // Again write "GEEKS" to buffer writer
        buffWriter.write(cbuf, 0, 5);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}

Output:
GEEKSFORGEEKS

References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(java.lang.String, int, int)
3. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(char%5B%5D, int, int)


Article Tags :