java.io
Class BufferedWriter

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.Writer sample code for java.io.Writer definition code for java.io.Writer 
      extended by java.io.BufferedWriter
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Flushable sample code for java.io.Flushable definition code for java.io.Flushable , Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable

public class BufferedWriter
extends Writer sample code for java.io.Writer definition code for java.io.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 sample code for java.io.PrintWriter definition code for java.io.PrintWriter , FileWriter sample code for java.io.FileWriter definition code for java.io.FileWriter , OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter

Field Summary
 
Fields inherited from class java.io.Writer sample code for java.io.Writer definition code for java.io.Writer
lock sample code for java.io.Writer.lock definition code for java.io.Writer.lock
 
Constructor Summary
BufferedWriter sample code for java.io.BufferedWriter.BufferedWriter(java.io.Writer) definition code for java.io.BufferedWriter.BufferedWriter(java.io.Writer) (Writer sample code for java.io.Writer definition code for java.io.Writer  out)
          Create a buffered character-output stream that uses a default-sized output buffer.
BufferedWriter sample code for java.io.BufferedWriter.BufferedWriter(java.io.Writer, int) definition code for java.io.BufferedWriter.BufferedWriter(java.io.Writer, int) (Writer sample code for java.io.Writer definition code for java.io.Writer  out, int sz)
          Create a new buffered character-output stream that uses an output buffer of the given size.
 
Method Summary
 void close sample code for java.io.BufferedWriter.close() definition code for java.io.BufferedWriter.close() ()
          Close the stream.
 void flush sample code for java.io.BufferedWriter.flush() definition code for java.io.BufferedWriter.flush() ()
          Flush the stream.
 void newLine sample code for java.io.BufferedWriter.newLine() definition code for java.io.BufferedWriter.newLine() ()
          Write a line separator.
 void write sample code for java.io.BufferedWriter.write(char[], int, int) definition code for java.io.BufferedWriter.write(char[], int, int) (char[] cbuf, int off, int len)
          Write a portion of an array of characters.
 void write sample code for java.io.BufferedWriter.write(int) definition code for java.io.BufferedWriter.write(int) (int c)
          Write a single character.
 void write sample code for java.io.BufferedWriter.write(java.lang.String, int, int) definition code for java.io.BufferedWriter.write(java.lang.String, int, int) (String sample code for java.lang.String definition code for java.lang.String  s, int off, int len)
          Write a portion of a String.
 
Methods inherited from class java.io.Writer sample code for java.io.Writer definition code for java.io.Writer
append sample code for java.io.Writer.append(char) definition code for java.io.Writer.append(char) , append sample code for java.io.Writer.append(java.lang.CharSequence) definition code for java.io.Writer.append(java.lang.CharSequence) , append sample code for java.io.Writer.append(java.lang.CharSequence, int, int) definition code for java.io.Writer.append(java.lang.CharSequence, int, int) , write sample code for java.io.Writer.write(char[]) definition code for java.io.Writer.write(char[]) , write sample code for java.io.Writer.write(java.lang.String) definition code for java.io.Writer.write(java.lang.String)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

BufferedWriter sample code for java.io.BufferedWriter(java.io.Writer) definition code for java.io.BufferedWriter(java.io.Writer)

public BufferedWriter(Writer sample code for java.io.Writer definition code for java.io.Writer  out)
Create a buffered character-output stream that uses a default-sized output buffer.

Parameters:
out - A Writer

BufferedWriter sample code for java.io.BufferedWriter(java.io.Writer, int) definition code for java.io.BufferedWriter(java.io.Writer, int)

public BufferedWriter(Writer sample code for java.io.Writer definition code for java.io.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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If sz is <= 0
Method Detail

write sample code for java.io.BufferedWriter.write(int) definition code for java.io.BufferedWriter.write(int)

public void write(int c)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write a single character.

Overrides:
write sample code for java.io.Writer.write(int) definition code for java.io.Writer.write(int) in class Writer sample code for java.io.Writer definition code for java.io.Writer
Parameters:
c - int specifying a character to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.BufferedWriter.write(char[], int, int) definition code for java.io.BufferedWriter.write(char[], int, int)

public void write(char[] cbuf,
                  int off,
                  int len)
           throws IOException sample code for java.io.IOException definition code for java.io.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.

Specified by:
write sample code for java.io.Writer.write(char[], int, int) definition code for java.io.Writer.write(char[], int, int) in class Writer sample code for java.io.Writer definition code for java.io.Writer
Parameters:
cbuf - A character array
off - Offset from which to start reading characters
len - Number of characters to write
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.BufferedWriter.write(java.lang.String, int, int) definition code for java.io.BufferedWriter.write(java.lang.String, int, int)

public void write(String sample code for java.lang.String definition code for java.lang.String  s,
                  int off,
                  int len)
           throws IOException sample code for java.io.IOException definition code for java.io.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 sample code for java.io.Writer.write(java.lang.String, int, int) definition code for java.io.Writer.write(java.lang.String, int, int) , which requires that an IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException be thrown.

Overrides:
write sample code for java.io.Writer.write(java.lang.String, int, int) definition code for java.io.Writer.write(java.lang.String, int, int) in class Writer sample code for java.io.Writer definition code for java.io.Writer
Parameters:
s - String to be written
off - Offset from which to start reading characters
len - Number of characters to be written
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

newLine sample code for java.io.BufferedWriter.newLine() definition code for java.io.BufferedWriter.newLine()

public void newLine()
             throws IOException sample code for java.io.IOException definition code for java.io.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 sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

flush sample code for java.io.BufferedWriter.flush() definition code for java.io.BufferedWriter.flush()

public void flush()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Flush the stream.

Specified by:
flush sample code for java.io.Flushable.flush() definition code for java.io.Flushable.flush() in interface Flushable sample code for java.io.Flushable definition code for java.io.Flushable
Specified by:
flush sample code for java.io.Writer.flush() definition code for java.io.Writer.flush() in class Writer sample code for java.io.Writer definition code for java.io.Writer
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

close sample code for java.io.BufferedWriter.close() definition code for java.io.BufferedWriter.close()

public void close()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Close the stream.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Specified by:
close sample code for java.io.Writer.close() definition code for java.io.Writer.close() in class Writer sample code for java.io.Writer definition code for java.io.Writer
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs