java.io

Class BufferedWriter

Implemented Interfaces:
Appendable, Closeable, Flushable

public class BufferedWriter
extends Writer

Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.

A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.

In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
 
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
Since:
JDK1.1
See Also:
PrintWriter, FileWriter, OutputStreamWriter

Field Summary

Fields inherited from class java.io.Writer

lock

Constructor Summary

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

Method Summary

void
close()
Close the stream.
void
flush()
Flush the stream.
void
newLine()
Write a line separator.
void
write(cbuf[] , int off, int len)
Write a portion of an array of characters.
void
write(int c)
Write a single character.
void
write(String s, int off, int len)
Write a portion of a String.

Methods inherited from class java.io.Writer

append, append, append, close, flush, write, write, write, write, write

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

BufferedWriter

public BufferedWriter(Writer out)
Create a buffered character-output stream that uses a default-sized output buffer.
Parameters:
out - A Writer
Usages and Demos :

View More Examples of BufferedWriter(Writer out)
   1: 
   2:       BufferedWriter bw = new BufferedWriter(fw);
   3: 
   4:       for(int i = 0; i < 12; i++) {
   5:         bw.write("Line " + i + "\n");

View Full Code Here
   1:     FileWriter fw = new FileWriter(f);
   2:     this.out = new BufferedWriter(fw);
   3:   }
   4: 
   5:   public void writeEntry(String message) throws IOException {

View Full Code Here
   1:       FileWriter fw = new FileWriter(args[0]);
   2:       BufferedWriter bw = new BufferedWriter(fw);
   3:       PrintWriter pw = new PrintWriter(bw, false);
   4: 
   5:       pw.println(true);

View Full Code Here
   1:       FileWriter fw = new FileWriter(args[1]);
   2:       BufferedWriter bw = new BufferedWriter(fw);
   3: 
   4:       String s;
   5:       while((s = br.readLine()) != null) {

View Full Code Here
   1:             FileWriter fw = new FileWriter(f);
   2:             BufferedWriter out = new BufferedWriter(fw);
   3:             out.write(
   4:                     "<tomcat-users>"
   5:                     + "<user name=\"" + _user + "\" password=\"" + _password + "\" roles=\"admin\" />"

View Full Code Here

BufferedWriter

public BufferedWriter(Writer out,
                      int sz)
Create a new buffered character-output stream that uses an output buffer of the given size.
Parameters:
out - A Writer
sz - Output-buffer size, a positive integer
Throws:
IllegalArgumentException - If sz is <= 0
Usages and Demos :

View More Examples of BufferedWriter(Writer out,int sz)
   1:       StringWriter sw = new StringWriter(24);
   2:       BufferedWriter bw = new BufferedWriter(sw, 12);
   3: 
   4:       String str = "There are a ton of great places to see live, original\n" +
   5:         "music in Chicago.  Places like Lounge Ax, Schuba's, the Empty\n" +

View Full Code Here
   1:       CharArrayWriter caw = new CharArrayWriter(24);
   2:       BufferedWriter bw = new BufferedWriter(caw, 12);
   3: 
   4:       String str = "I used to live right behind this super-cool bar in\n" +
   5:         "Chicago called Lounge Ax.  They have the best music of pretty\n" +

View Full Code Here
   1:         StringWriter sw = new StringWriter();
   2:         BufferedWriter out = new BufferedWriter(sw, this.bufferSize);
   3:         Map outputMap = new HashMap();
   4:         while ((str = in.readLine()) != null) {
   5:             for (int i = 0; i < oldText.length; i++) {

View Full Code Here
   1:     public WriterOutputBuffer(Writer writer) {
   2:     _writer = new BufferedWriter(writer, BUFFER_SIZE);
   3:     }
   4: 
   5:     public String close() {

View Full Code Here
   1:         StringWriter sw = new StringWriter();
   2:         BufferedWriter out = new BufferedWriter(sw, this.bufferSize);
   3:         String[] oldText = this.getOldText();
   4:         String[] replacementText = this.getReplacementText();
   5:         out.write("<?xml version=\"1.0\"?>");

View Full Code Here

Method Details

close

public void close()
            throws IOException
Close the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of close()
   1:         
   2:         BufferedWriter bw=new BufferedWriter(new FileWriter("table1.txt"));
   3:         for(int i=1;i<=10000;i++)
   4:         ...
   5:         }
   6:         bw.close();
   7:         System.out.print("finished!");
   8:     }

View Full Code Here
   1: 
   2:       BufferedWriter bw = new BufferedWriter(fw);
   3: 
   4:         ...
   5: 
   6:       bw.close();
   7:     }
   8:     catch(Exception e) {
   9:       System.out.println("Exception: " + e);

View Full Code Here
   1:          BufferedReader in =new BufferedReader(new FileReader("c:\\Java\\test.txt"));
   2:          BufferedWriter out =new BufferedWriter(new FileWriter("c:\\Java\\test2.txt"));
   3:          while((s = in.readLine())!= null) {
   4:         ...
   5:          out.flush();;
   6:          in.close();
   7:         ...
   8:          out.close();
   9:       }

View Full Code Here
   1:   public static void write( XMLDocument doc, CLOB theClob ) throws Exception {
   2:     BufferedWriter out = new BufferedWriter(theClob.getCharacterOutputStream(),
   3:                                             theClob.getChunkSize());
   4:         ...
   5:     doc.print(new PrintWriter(out));
   6:     out.close();
   7:   }
   8: }

View Full Code Here
   1:   public static void main(String[] args) {
   2:     BufferedWriter out = null;
   3: 
   4:         ...
   5:     try {
   6:       out = new BufferedWriter(new OutputStreamWriter(
   7:           new FileOutputStream("Exercise26_11.gb"), "GB18030"));
   8:         ...
   9:         out.flush();
  10:         out.close();
  11:       }

View Full Code Here

flush

public void flush()
            throws IOException
Flush the stream.
Specified by:
flush in interface Flushable
Overrides:
flush in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of flush()
   1:          BufferedReader in =new BufferedReader(new FileReader("c:\\Java\\test.txt"));
   2:          BufferedWriter out =new BufferedWriter(new FileWriter("c:\\Java\\test2.txt"));
   3:          while((s = in.readLine())!= null) {
   4:         ...
   5:          }
   6:          out.flush();;
   7:          in.close();
   8:          out.close();
   9:       }

View Full Code Here
   1:   public static void main(String[] args) {
   2:     BufferedWriter out = null;
   3: 
   4:         ...
   5:     try {
   6:       out = new BufferedWriter(new OutputStreamWriter(
   7:           new FileOutputStream("Exercise26_11.gb"), "GB18030"));
   8:         ...
   9:       try {
  10:         out.flush();
  11:         out.close();

View Full Code Here
   1:       FileWriter fw = new FileWriter(args[1]);
   2:       BufferedWriter bw = new BufferedWriter(fw);
   3: 
   4:         ...
   5: 
   6:       bw.flush();
   7: 
   8:       fr.close();
   9:       fw.close();

View Full Code Here
   1: import java.io.InputStreamReader;
   2: import java.io.BufferedWriter;
   3: import java.io.BufferedReader;
   4:         ...
   5:         Socket s = new Socket("localhost", 3006);
   6:         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
   7:         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
   8:         ...
   9:         bw.newLine();
  10:         bw.flush();
  11:         System.out.println(br.readLine());
  12:         ...
  13:         bw.newLine();
  14:         bw.flush();

View Full Code Here
   1: import java.io.InputStreamReader;
   2: import java.io.BufferedWriter;
   3: import java.io.BufferedReader;
   4:         ...
   5:         Socket s = new Socket("localhost", 3006);
   6:         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
   7:         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
   8:         ...
   9:         bw.newLine();
  10:         bw.flush();
  11:         System.out.println(br.readLine());
  12:         ...
  13:         bw.newLine();
  14:         bw.flush();

View Full Code Here

newLine

public void newLine()
            throws IOException
Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of newLine()
   1: import java.io.InputStreamReader;
   2: import java.io.BufferedWriter;
   3: import java.io.BufferedReader;
   4:         ...
   5:         Socket s = new Socket("localhost", 3006);
   6:         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
   7:         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
   8:         ...
   9:         bw.write("HELLO");
  10:         bw.newLine();
  11:         bw.flush();
  12:         ...
  13:         bw.write("TOD");
  14:         bw.newLine();

View Full Code Here
   1: import java.io.InputStreamReader;
   2: import java.io.BufferedWriter;
   3: import java.io.BufferedReader;
   4:         ...
   5:         Socket s = new Socket("localhost", 3006);
   6:         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
   7:         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
   8:         ...
   9:         bw.write("HELLO");
  10:         bw.newLine();
  11:         bw.flush();
  12:         ...
  13:         bw.write("TOD");
  14:         bw.newLine();

View Full Code Here
   1:       BufferedReader br;
   2:       BufferedWriter bw;
   3:       String         line;
   4:         ...
   5:       br    = new BufferedReader(new FileReader(argv[0]));
   6:       bw    = new BufferedWriter(new FileWriter(argv[1]));
   7: 
   8:         ...
   9:          bw.write(line);
  10:          bw.newLine();
  11:       }

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.FileWriter;
   4:         ...
   5:     private static final boolean DEBUG = true;
   6:     private static BufferedWriter out = null;
   7:     private static final String logfile = "log.txt";
   8:         ...
   9:             if (null == out) {
  10:                 out = new BufferedWriter(new FileWriter(logfile));
  11:             }
  12:         ...
  13:             out.write("X:" + func);
  14:             out.newLine();

View Full Code Here
   1: import java.io.PrintWriter;
   2: import java.io.BufferedWriter;
   3: import java.io.IOException;
   4:         ...
   5:       long start = System.currentTimeMillis();
   6:       BufferedWriter out = new BufferedWriter(new FileWriter("CharWriterDemo.tmp"));
   7:       for (int i = 0; i < args.length; i++) {
   8:         ...
   9:          out.write(args[i]);
  10:          out.newLine();
  11:       }

View Full Code Here

write

public void write(cbuf[] ,
                  int off,
                  int len)
            throws IOException
Write a portion of an array of characters.

Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant BufferedWriters will not copy data unnecessarily.

Overrides:
write in interface Writer
Parameters:
off - Offset from which to start reading characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(cbuf[] ,int off,int len)
   1:         
   2:         BufferedWriter bw=new BufferedWriter(new FileWriter("table1.txt"));
   3:         for(int i=1;i<=10000;i++)
   4:         ...
   5:         {
   6:             bw.write("user"+i+","+i+","+"?????\n"); 
   7:         }
   8:         bw.close();
   9:         System.out.print("finished!");

View Full Code Here
   1:       StringWriter sw = new StringWriter(24);
   2:       BufferedWriter bw = new BufferedWriter(sw, 12);
   3: 
   4:         ...
   5: 
   6:       bw.write(buf, 0, 5);
   7:       if (sw.toString().length() != 0)
   8:         ...
   9:         }
  10:       bw.write(buf, 5, 8);
  11:         ...
  12:       bw.write(buf, 13, 12);

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.FileOutputStream;
   4:         ...
   5:   int count = 0;
   6:   private BufferedWriter lineFileOut=null;
   7:   private DocMaker docMaker;
   8:         ...
   9:         throw new Exception("line.file.out must be set");
  10:       lineFileOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8"));
  11:     }
  12:         ...
  13:     if (body != null) {
  14:       lineFileOut.write(title, 0, title.length());

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.File;
   4:         ...
   5:         }        
   6:         BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );
   7:         ...
   8:         writer.write("_=id:Integer,*geom:Geometry,name:String"); writer.newLine();
   9:         writer.write("fid1=1|LINESTRING(0 0,10 10)|jody"); writer.newLine();

View Full Code Here
   1:         BufferedInputStream is = null;
   2:         BufferedWriter os = null;
   3: 
   4:         ...
   5: 
   6:             os = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(dataDir + outputFileName) ) );
   7: 
   8:         ...
   9: 
  10:             os.write("<CLASS>,<ATTRIBUTE>,<DOMAIN>" + lineSeparator);

View Full Code Here

write

public void write(int c)
            throws IOException
Write a single character.
Overrides:
write in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(int c)
   1: import java.awt.*;
   2: import java.io.BufferedWriter;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:     public void writeDatafile(BufferedWriter writer) throws IOException{
   7:         for(Double point:data){
   8:         ...
   9:             writer.write(""+point.x+" "+point.y+"\n");
  10:         }
  11:         ...
  12:     
  13:     public void writeDisplayCommand(BufferedWriter writer, String datafile) throws IOException{

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.IOException;
   4: 
   5: import de.java2html.javasource.JavaSourceType;

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     BufferedWriter writer;
   7:     
   8:         ...
   9:     private void attribute( String name, String value ) throws IOException {
  10:         writer.write( " " + name + "=\"" + value + "\"" );
  11:     }
  12:         ...
  13:         if ( root ) {
  14:             writer = new BufferedWriter( new OutputStreamWriter( output ) );

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.File;
   4:         ...
   5: 
   6:         BufferedWriter writer = null;
   7: 
   8:         ...
   9: 
  10:             writer = new BufferedWriter(osw);
  11: 
  12:         ...
  13: 
  14:                     writer.write(header + "\t");

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:     private final BufferedWriter bw;
   7: 
   8:         ...
   9:         this.prefs = prefs;
  10:         if (w instanceof BufferedWriter) {
  11:             this.bw = (BufferedWriter) w;
  12:         ...
  13:         if (prefs.isUserNode()) {
  14:             bw.write("user");

View Full Code Here

write

public void write(String s,
                  int off,
                  int len)
            throws IOException
Write a portion of a String.

If the value of the len parameter is negative then no characters are written. This is contrary to the specification of this method in the superclass, which requires that an IndexOutOfBoundsException be thrown.

Overrides:
write in interface Writer
Parameters:
s - String to be written
off - Offset from which to start reading characters
len - Number of characters to be written
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(String s,int off,int len)
   1:         
   2:         BufferedWriter bw=new BufferedWriter(new FileWriter("table1.txt"));
   3:         for(int i=1;i<=10000;i++)
   4:         ...
   5:         {
   6:             bw.write("user"+i+","+i+","+"?????\n"); 
   7:         }
   8:         bw.close();
   9:         System.out.print("finished!");

View Full Code Here
   1:       StringWriter sw = new StringWriter(24);
   2:       BufferedWriter bw = new BufferedWriter(sw, 12);
   3: 
   4:         ...
   5: 
   6:       bw.write(buf, 0, 5);
   7:       if (sw.toString().length() != 0)
   8:         ...
   9:         }
  10:       bw.write(buf, 5, 8);
  11:         ...
  12:       bw.write(buf, 13, 12);

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.FileOutputStream;
   4:         ...
   5:   int count = 0;
   6:   private BufferedWriter lineFileOut=null;
   7:   private DocMaker docMaker;
   8:         ...
   9:         throw new Exception("line.file.out must be set");
  10:       lineFileOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8"));
  11:     }
  12:         ...
  13:     if (body != null) {
  14:       lineFileOut.write(title, 0, title.length());

View Full Code Here
   1: 
   2: import java.io.BufferedWriter;
   3: import java.io.File;
   4:         ...
   5:         }        
   6:         BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );
   7:         ...
   8:         writer.write("_=id:Integer,*geom:Geometry,name:String"); writer.newLine();
   9:         writer.write("fid1=1|LINESTRING(0 0,10 10)|jody"); writer.newLine();

View Full Code Here
   1:         BufferedInputStream is = null;
   2:         BufferedWriter os = null;
   3: 
   4:         ...
   5: 
   6:             os = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(dataDir + outputFileName) ) );
   7: 
   8:         ...
   9: 
  10:             os.write("<CLASS>,<ATTRIBUTE>,<DOMAIN>" + lineSeparator);

View Full Code Here