Writing data to a file using BufferedWriter class in Java


The BufferedWriter class of Java is used to write a stream of characters to the specified destination (character-output stream). It initially stores all the characters in a buffer and pushes the contents of the buffer to the destination, making the writing of characters, arrays and Strings efficient.

You can specify the required size of the buffer at the time of instantiating this class.

Example

In the following Java program, we are trying to print a line on the console (Standard Output Stream). Here we are invoking the write() method by passing the required String.

 Live Demo

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class BufferedWriterExample {
   public static void main(String args[]) throws IOException {
      //Instantiating the OutputStreamWriter class
      OutputStreamWriter out = new OutputStreamWriter(System.out);
      //Instantiating the BufferedWriter
      BufferedWriter writer = new BufferedWriter(out);
      //Writing data to the console
      writer.write("Hello welcome to Tutorialspoint");
      writer.flush();
   }
}

Output

Hello welcome to Tutorialspoint

Updated on: 12-Sep-2019

621 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements