BufferedWriter class

                                   
    java.lang.Object                              
     |                             
     |--java.io.Writer                          
         |                         
         |--java.io.BufferedWriter                      
                                   

Writes text to a character-output stream with buffer.

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

ReturnMethodSummary
voidclose()Closes the stream, flushing it first.
voidflush()Flushes the stream.
voidnewLine()Writes a line separator.
voidwrite(char[] cbuf, int off, int len)Writes a portion of an array of characters.
voidwrite(int c)Writes a single character.
voidwrite(String s, int off, int len)Writes a portion of a String.
Revised from Open JDK source code

Create BufferedWriter from standard System.out


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String strLine = in.readLine();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write(strLine, 0, strLine.length());
    out.flush();
    in.close();
    out.close();
  }
}

Write String to a file with BufferedWriter


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

public class Main {
  public static void main(String[] args) throws Exception {
    Writer writer = null;

    String text = "This is a text file";

    File file = new File("write.txt");
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(text);
    writer.close();

  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.