Menu
Topics Index
...
`


Input/Output: Exploring java.io > Character Streams >
Siva Nookala - 20 Feb 2017
BufferedWriter writes text to a character-output stream, to provide buffering for the efficient writing of single characters, arrays and strings.
  • The buffer size may be specified, or the default size may be used.
  • A Writer sends its output immediately to the underlying character or byte stream. It is advisable to wrap a BufferedWriter around any Writer whose Write() operations may be costly, such as Java FileWriter and OutputStreamWriters. For example,
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));
    this will output to the test.txt file.

BufferedWriter Constructors:
ConstructorDescription
BufferedWriter(Writer out)Creates a buffered character-output stream that uses a default-sized output buffer.
BufferedWriter(Writer out, int size)Creates a new buffered character-output stream that uses an output buffer of the given size.

BufferedWriter Methods:
MethodsDescription
void close()Closes the stream, flushing it first.
void flush()Flushes the stream.
(forces any buffered output bytes to be written out)
void newLine()Writes a line separator.
void write(char[] c, int from, int length)Writes a portion of an array of characters.
void write(int c)Writes a single character.
void write(String s, int from, int length)Writes a portion of a String.

Java.io.BufferedWriter class inherits methods from the following classes:
  • Java.io.Writer
  • Java.io.Object

Example program which creates a file and writes some data into it.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TestA {
    public static void main(String[] args) {
        try {

            String content = "This is the content to write into file.";

            File file = new File("F:\\filename.txt");

            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content); // LINE A
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Output:
Done
Description: It checks for the folder filename.txt in F drive if it is not found then creates a new filename.txt file and content will be written to the txt file and closes the BufferWriter.


Program for close, flush, newLine, write(char[] c, int from, int length), write(int c), write(String s, int from, int length).
BufferedWriter
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

class BufferedWriterExampleTest
{
    public static void main(String arg[])
    {
        String s = "Hello World!";
                char[] c = "ABCDEFGHIJKLMN".toCharArray();
                
                try {
                    StringWriter sw = new StringWriter();
                    BufferedWriter bw = new BufferedWriter(sw);
        
                    bw.write(s, 0, 5); // LINE B
                    bw.newLine(); // LINE C
                    bw.write(c, 8, 3); //LINE D
                    bw.flush(); // LINE E
                    System.out.println(sw.getBuffer());
                    bw.close(); // LINE F
                    bw.append(s); // ERROR OCCURS
                    
                } catch (Exception e) {
                    System.out.println("Stream is closed so you cant append.");
                }    
    }
}
OUTPUT

Hello
IJK
Stream is closed so you cant append.

DESCRIPTION

In this program, We have a string and a character array, at LINE B we are writing 5 characters of string, at LINE C a new line, 3 characters from position 9 of character array to buffer-writer. At LINE E flushing the whole buffer-writer to string writer and printing the written data. At LINE F the buffer-writer is closed and in next line trying to append the buffer-writer then exception occurs.

THINGS TO TRY
  • Try to execute these two programs in eclipse.
  • Try to create a new file and add data into in using all methods.
  • Try to create different files like pdf, doc etc.,

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.