JavaBufferedWriter

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java BufferedWriter Class

Java BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It inherits Writer class. The buffering characters are used for providing the efficient writing of single arrays, characters, and strings.

Class declaration

Let's see the declaration for Java.io.BufferedWriter class:

  1. public class BufferedWriter extends Writer  

Class constructors

Constructor

Description

BufferedWriter(Writer wrt)

It is used to create a buffered character output stream that uses the default size for an output buffer.

BufferedWriter(Writer wrt, int size)

It is used to create a buffered character output stream that uses the specified size for an output buffer.

Class methods

Method

Description

void newLine()

It is used to add a new line by writing a line separator.

void write(int c)

It is used to write a single character.

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

It is used to write a portion of an array of characters.

void write(String s, int off, int len)

It is used to write a portion of a string.

void flush()

It is used to flushes the input stream.

void close()

It is used to closes the input stream

Example of Java BufferedWriter

Let's see the simple example of writing the data to a text file testout.txt using Java BufferedWriter.

  1. package com.javatpoint;  
  2. import java.io.*;  
  3. public class BufferedWriterExample {  
  4. public static void main(String[] args) throws Exception {     
  5.     FileWriter writer = new FileWriter("D: estout.txt");  
  6.     BufferedWriter buffer = new BufferedWriter(writer);  
  7.     buffer.write("Welcome to javaTpoint.");  
  8.     buffer.close();  
  9.     System.out.println("Success");  
  10.     }  
  11. }  

Output:

success

testout.txt:

Welcome to tutorialtours.