Java.io - SPLessons

Java.io BufferedWriter

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io BufferedWriter

Java.io BufferedWriter

shape Introduction

The Java.io BufferedWriter is the class used for writing the characters into a directory or file. The buffered writer performance is little bit faster compared to other writers like writing the files or data into disk drive and in many more applications.

shape Description

The Java.io BufferedWriter is similar to that buffered output stream with tiny difference that it writes stream of bytes like data related to images, where as the buffered writer writes the characters like in the text form. The main feature of this class is to write block of characters or data at a time instead of writing data character by character.In Java.io BufferedWriter class, contains one method called new line() method used to write the textual line at a time in characters.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io/BufferedWriter class is declared below: Public class BufferedWriter extends Writer

Class Constructors

shape Table

Constructor Description
BufferedWriter (Writer out) The function of this constructor is to create the buffered writer object for output character stream with default buffer size.
BufferedWriter (writes out, int n) The function of this constructor is to create the buffered writer object for output character stream with represented buffer size.

Class Methods

shape Table

Method Description
void close() The function of this method is to close the current output stream after flushing.
void flush () The function of this method is to flush the output stream.
void newLine () The function of this method is to write a new line of characters
void write (string s int off, int len) The function of this method is to write a part of string

Inherited Methods

shape Description

From the following classes, methods are inherited the buffered writer class.
  • Java.io.object
  • Java.io.Writer

shape Examples

Usage of void close() method. [c]import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; public class BufferedWriter { public static void main(String[] args) throws IOException { StringWriter sw = null; BufferedWriter bw = null; try{ // creating a new string writer sw = new StringWriter(); //creating a new buffered writer bw = new BufferedWriter(sw); // To append character. bw.append("1"); // To close the writer bw.close(); // print before going to appending one more character System.out.println(sw.getBuffer()); // appending after closing which will throw error bw.append("2"); // print after going to appending one more character System.out.println(sw.getBuffer()); }catch(IOException e) { // if I/O error occurs System.out.print("not append, buffered writer is closed"); }finally { // releases all the system resources associated with this stream if(sw!=null) sw.close(); if(bw!=null) bw.close(); } } }[/c] Output Following is the result will be displayed. [c]not append, buffered writer is closed[/c] Usage of void flush() method. [c]import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; public class BufferedWriter { public static void main(String[] args) throws IOException { StringWriter sw = null; BufferedWriter bw = null; String letters = "ABCDEFG"; try{ // creating a new string writer sw = new StringWriter(); //creating a new buffered writer bw = new BufferedWriter(sw); // Represents each character in the string for (char c: letters.toCharArray()) { // To append character to the writer bw.append(c); // flush the characters to the considered stream bw.flush(); // It prints string buffer from string writer System.out.println(sw.getBuffer()); } }catch(IOException e) { // if I/O error occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(sw!=null) sw.close(); if(bw!=null) bw.close(); } } }[/c] Output Following is the result will be displayed. [c] A AB ABC ABCD ABCDE ABCDEF ABCDEFG[/c]

Summary

shape Key Points

  • The Java.io BufferedWriter is the class used for writing the characters into a directory or file.
  • The buffered writer performance is little bit faster compared to other writers like writing the files or data into disk drive and in many more applications.
  • The main feature of this class is to write block of characters or data at a time instead of writing data character by character.