Java - BufferedWriter write(int c) method



Description

The Java BufferedWriter write(char[] cbuf, int off, int len) method writes a section of character buffer to the writer. Offset off from which to start reading characters and the len is the length of the section from the character buffer. This method writes characters starting from the index off (offset) in the array and writes up to len characters. This method is useful when you want to write a specific subset of characters from a larger character array.

Declaration

Following is the declaration for java.io.BufferedWriter.write(int c) method:

public void write(int c)

Parameters

  • c− integer specifying a character to write

Return Value

This method does not return any value.

Exception

  • IOException− if an I/O error occurs.

Example - Using write(int c) method

The following example shows the usage of Java BufferedWriter write(int c) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class BufferedWriterDemo {
   public static void main(String[] args) throws IOException {
      
      StringWriter sw = null;
      BufferedWriter bw = null;
      
      try{
         // create string writer
         sw = new StringWriter();
         
         //create buffered writer
         bw = new BufferedWriter(sw);
         
         // integer range represents alphabets in uppercase
         for(int i = 65; i<=90; i++)
         {
            bw.write(i);
         }
         
         // forces out the characters to string writer
         bw.flush();
         
         // string buffer is created
         StringBuffer sb = sw.getBuffer();
         
         // prints the string
         System.out.println(sb);
            
      }catch(IOException e){
         // if I/O error occurs
         e.printStackTrace();
      }finally{
         // releases any system resources associated with the stream
         if(sw!=null)
            sw.close();
         if(bw!=null)
            bw.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example - Writing Characters One by One

The following example shows the usage of Java BufferedWriter write(int c) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {
   public static void main(String[] args) {
      String filePath = "example.txt";

      try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
         // Write individual characters using write(int c)
         writer.write('H'); // Writes the character 'H'
         writer.write('e'); // Writes the character 'e'
         writer.write('l'); // Writes the character 'l'
         writer.write('l'); // Writes the character 'l'
         writer.write('o'); // Writes the character 'o'
         writer.write('!');

         System.out.println("Characters written to the file.");
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Characters written to the file.

Output in File (example.txt)

Hello!

Explanation

  • The write(int c) method is used to write characters one by one to the file.

  • Each character ('H', 'e', 'l', 'l', 'o', '!') is passed as an argument to the write(int c) method.

  • The characters are stored in the buffer and written to the file when the buffer is flushed (automatically or when the writer is closed).

Example - Writing Unicode Characters

The following example shows the usage of Java BufferedWriter write(int c) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {
    public static void main(String[] args) {
        String filePath = "example.txt";

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            // Write individual Unicode characters
            writer.write(0x2602); // Unicode for '' (umbrella symbol)
            writer.write(0x1F600); // Unicode for  (smiley face)
            writer.write(0x2665); // Unicode for '' (heart symbol)

            System.out.println("Unicode characters written to the file.");
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}

Output

Let us compile and run the above program, this will produce the following result −

Unicode characters written to the file.

Output in File (example.txt)


Explanation

  • The write(int c) method is used to write Unicode characters to the file.

  • Unicode code points (0x2602 for , 0x1F600 for , 0x2665 for ) are passed as arguments to the write(int c) method.

  • Only the lower 16 bits of the integers are written, so characters beyond the basic multilingual plane (e.g., emojis) may not appear correctly unless the environment supports them.

Key Points About write(int c)

  • Single Character Writing− The method writes one character at a time, represented as an integer.

  • Unicode Support− You can use Unicode values to write special symbols or characters.

  • Efficient for Simple Use Cases− Useful when writing a few characters or symbols individually.

When to Use This write(int c)

  • When writing a small number of individual characters.

  • To write specific Unicode characters or symbols to a file.

These examples showcase practical use cases of the write(int c) method, demonstrating its ability to handle both ASCII and Unicode characters.

java_io_bufferedwriter.htm
Advertisements