Open In App

BufferedWriter write() method in Java with Examples

Last Updated : 28 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • s – It represents the string, the part of which is to written.
  • off – It represents the index in the string from which writing is started.
  • len – It represents the length of portion of string to be written.

Return value: This method does not return any value.

Exceptions:

  • IndexOutOfBoundsException – This method throws IndexOutOfBoundsException if index passed is negative or passed length of portion is greater than the length of the given string.
  • IOException – This method throws IOException if IO error occurs.

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:

  • cbuf – It represents the array of characters, the part of which is to written.
  • off – It represents the index of the array from which writing is started.
  • len – It represents the length of portion of array to be written.

Return value: This method does not return any value.

Exceptions:

  • IndexOutOfBoundsException – This method throws IndexOutOfBoundsException if index passed is negative or passed length of portion is greater than the length of the given character array.
  • IOException – This method throws IOException if IO error occurs.

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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads