Buffered Reader And Buffered Writer


Buffered Reader

This class is used to read the text from a character-based input stream. Data can be read line by line by using readLine() method. It helps to increase the performance. This class inherits methods from the Reader class.

Syntax for declaring java.io.BufferedReader class:

public class BufferedReader extends Reader  

Example :

import java.io.*; public class BufferedReaderExample { public static void main(String args[])throws Exception{ FileReader fr=new FileReader("knowledge2life.txt"); BufferedReader br=new BufferedReader(fr); int i; while((i=br.read())!=-1){ System.out.print((char)i); } br.close(); fr.close(); } }

OUTPUT:

Welcome to Knowledge2life.

Buffered Writer

This class is used to provide buffering for Writer instances. It helps to increase the performance. This class inherits methods from the Writer class.

Syntax for declaring java.io.BufferedWriter class:

public class BufferedWriter extends Writer

Example :

package com.javatpoint; import java.io.*; public class BufferedWriterExample { public static void main(String[] args) throws Exception { FileWriter writer = new FileWriter("Knowledge2life.txt"); BufferedWriter buffer = new BufferedWriter(writer); buffer.write("Welcome to Knowledge2life."); buffer.close(); System.out.println("Success"); } }

OUTPUT:

Success