java.io
Class PrintWriter

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.PrintWriter
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 PrintWriter
extends Writer sample code for java.io.Writer definition code for java.io.Writer

Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream . It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

Unlike the PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError() sample code for java.io.PrintWriter.checkError() definition code for java.io.PrintWriter.checkError() .

Since:
JDK1.1

Field Summary
protected  Writer sample code for java.io.Writer definition code for java.io.Writer out sample code for java.io.PrintWriter.out definition code for java.io.PrintWriter.out
          The underlying character-output stream of this PrintWriter.
 
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
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.File) definition code for java.io.PrintWriter.PrintWriter(java.io.File) (File sample code for java.io.File definition code for java.io.File  file)
          Creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.File, java.lang.String) definition code for java.io.PrintWriter.PrintWriter(java.io.File, java.lang.String) (File sample code for java.io.File definition code for java.io.File  file, String sample code for java.lang.String definition code for java.lang.String  csn)
          Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.OutputStream) definition code for java.io.PrintWriter.PrintWriter(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Create a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.OutputStream, boolean) definition code for java.io.PrintWriter.PrintWriter(java.io.OutputStream, boolean) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, boolean autoFlush)
          Create a new PrintWriter from an existing OutputStream.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.lang.String) definition code for java.io.PrintWriter.PrintWriter(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  fileName)
          Creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.lang.String, java.lang.String) definition code for java.io.PrintWriter.PrintWriter(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  fileName, String sample code for java.lang.String definition code for java.lang.String  csn)
          Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.Writer) definition code for java.io.PrintWriter.PrintWriter(java.io.Writer) (Writer sample code for java.io.Writer definition code for java.io.Writer  out)
          Create a new PrintWriter, without automatic line flushing.
PrintWriter sample code for java.io.PrintWriter.PrintWriter(java.io.Writer, boolean) definition code for java.io.PrintWriter.PrintWriter(java.io.Writer, boolean) (Writer sample code for java.io.Writer definition code for java.io.Writer  out, boolean autoFlush)
          Create a new PrintWriter.
 
Method Summary
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter append sample code for java.io.PrintWriter.append(char) definition code for java.io.PrintWriter.append(char) (char c)
          Appends the specified character to this writer.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter append sample code for java.io.PrintWriter.append(java.lang.CharSequence) definition code for java.io.PrintWriter.append(java.lang.CharSequence) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq)
          Appends the specified character sequence to this writer.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter append sample code for java.io.PrintWriter.append(java.lang.CharSequence, int, int) definition code for java.io.PrintWriter.append(java.lang.CharSequence, int, int) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq, int start, int end)
          Appends a subsequence of the specified character sequence to this writer.
 boolean checkError sample code for java.io.PrintWriter.checkError() definition code for java.io.PrintWriter.checkError() ()
          Flush the stream if it's not closed and check its error state.
 void close sample code for java.io.PrintWriter.close() definition code for java.io.PrintWriter.close() ()
          Close the stream.
 void flush sample code for java.io.PrintWriter.flush() definition code for java.io.PrintWriter.flush() ()
          Flush the stream.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter format sample code for java.io.PrintWriter.format(java.util.Locale, java.lang.String, java.lang.Object...) definition code for java.io.PrintWriter.format(java.util.Locale, java.lang.String, java.lang.Object...) (Locale sample code for java.util.Locale definition code for java.util.Locale  l, String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          Writes a formatted string to this writer using the specified format string and arguments.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter format sample code for java.io.PrintWriter.format(java.lang.String, java.lang.Object...) definition code for java.io.PrintWriter.format(java.lang.String, java.lang.Object...) (String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          Writes a formatted string to this writer using the specified format string and arguments.
 void print sample code for java.io.PrintWriter.print(boolean) definition code for java.io.PrintWriter.print(boolean) (boolean b)
          Print a boolean value.
 void print sample code for java.io.PrintWriter.print(char) definition code for java.io.PrintWriter.print(char) (char c)
          Print a character.
 void print sample code for java.io.PrintWriter.print(char[]) definition code for java.io.PrintWriter.print(char[]) (char[] s)
          Print an array of characters.
 void print sample code for java.io.PrintWriter.print(double) definition code for java.io.PrintWriter.print(double) (double d)
          Print a double-precision floating-point number.
 void print sample code for java.io.PrintWriter.print(float) definition code for java.io.PrintWriter.print(float) (float f)
          Print a floating-point number.
 void print sample code for java.io.PrintWriter.print(int) definition code for java.io.PrintWriter.print(int) (int i)
          Print an integer.
 void print sample code for java.io.PrintWriter.print(long) definition code for java.io.PrintWriter.print(long) (long l)
          Print a long integer.
 void print sample code for java.io.PrintWriter.print(java.lang.Object) definition code for java.io.PrintWriter.print(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Print an object.
 void print sample code for java.io.PrintWriter.print(java.lang.String) definition code for java.io.PrintWriter.print(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Print a string.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter printf sample code for java.io.PrintWriter.printf(java.util.Locale, java.lang.String, java.lang.Object...) definition code for java.io.PrintWriter.printf(java.util.Locale, java.lang.String, java.lang.Object...) (Locale sample code for java.util.Locale definition code for java.util.Locale  l, String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          A convenience method to write a formatted string to this writer using the specified format string and arguments.
 PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter printf sample code for java.io.PrintWriter.printf(java.lang.String, java.lang.Object...) definition code for java.io.PrintWriter.printf(java.lang.String, java.lang.Object...) (String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          A convenience method to write a formatted string to this writer using the specified format string and arguments.
 void println sample code for java.io.PrintWriter.println() definition code for java.io.PrintWriter.println() ()
          Terminate the current line by writing the line separator string.
 void println sample code for java.io.PrintWriter.println(boolean) definition code for java.io.PrintWriter.println(boolean) (boolean x)
          Print a boolean value and then terminate the line.
 void println sample code for java.io.PrintWriter.println(char) definition code for java.io.PrintWriter.println(char) (char x)
          Print a character and then terminate the line.
 void println sample code for java.io.PrintWriter.println(char[]) definition code for java.io.PrintWriter.println(char[]) (char[] x)
          Print an array of characters and then terminate the line.
 void println sample code for java.io.PrintWriter.println(double) definition code for java.io.PrintWriter.println(double) (double x)
          Print a double-precision floating-point number and then terminate the line.
 void println sample code for java.io.PrintWriter.println(float) definition code for java.io.PrintWriter.println(float) (float x)
          Print a floating-point number and then terminate the line.
 void println sample code for java.io.PrintWriter.println(int) definition code for java.io.PrintWriter.println(int) (int x)
          Print an integer and then terminate the line.
 void println sample code for java.io.PrintWriter.println(long) definition code for java.io.PrintWriter.println(long) (long x)
          Print a long integer and then terminate the line.
 void println sample code for java.io.PrintWriter.println(java.lang.Object) definition code for java.io.PrintWriter.println(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  x)
          Print an Object and then terminate the line.
 void println sample code for java.io.PrintWriter.println(java.lang.String) definition code for java.io.PrintWriter.println(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  x)
          Print a String and then terminate the line.
protected  void setError sample code for java.io.PrintWriter.setError() definition code for java.io.PrintWriter.setError() ()
          Indicate that an error has occurred.
 void write sample code for java.io.PrintWriter.write(char[]) definition code for java.io.PrintWriter.write(char[]) (char[] buf)
          Write an array of characters.
 void write sample code for java.io.PrintWriter.write(char[], int, int) definition code for java.io.PrintWriter.write(char[], int, int) (char[] buf, int off, int len)
          Write A Portion of an array of characters.
 void write sample code for java.io.PrintWriter.write(int) definition code for java.io.PrintWriter.write(int) (int c)
          Write a single character.
 void write sample code for java.io.PrintWriter.write(java.lang.String) definition code for java.io.PrintWriter.write(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Write a string.
 void write sample code for java.io.PrintWriter.write(java.lang.String, int, int) definition code for java.io.PrintWriter.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.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)
 

Field Detail

out sample code for java.io.PrintWriter.out

protected Writer sample code for java.io.Writer definition code for java.io.Writer  out
The underlying character-output stream of this PrintWriter.

Since:
1.2
Constructor Detail

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

public PrintWriter(Writer sample code for java.io.Writer definition code for java.io.Writer  out)
Create a new PrintWriter, without automatic line flushing.

Parameters:
out - A character-output stream

PrintWriter sample code for java.io.PrintWriter(java.io.Writer, boolean) definition code for java.io.PrintWriter(java.io.Writer, boolean)

public PrintWriter(Writer sample code for java.io.Writer definition code for java.io.Writer  out,
                   boolean autoFlush)
Create a new PrintWriter.

Parameters:
out - A character-output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer

PrintWriter sample code for java.io.PrintWriter(java.io.OutputStream) definition code for java.io.PrintWriter(java.io.OutputStream)

public PrintWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Create a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

PrintWriter sample code for java.io.PrintWriter(java.io.OutputStream, boolean) definition code for java.io.PrintWriter(java.io.OutputStream, boolean)

public PrintWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                   boolean autoFlush)
Create a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

PrintWriter sample code for java.io.PrintWriter(java.lang.String) definition code for java.io.PrintWriter(java.lang.String)

public PrintWriter(String sample code for java.lang.String definition code for java.lang.String  fileName)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException 
Creates a new PrintWriter, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the default charset sample code for java.nio.charset.Charset.defaultCharset definition code for java.nio.charset.Charset.defaultCharset for this instance of the Java virtual machine.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(fileName) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
Since:
1.5

PrintWriter sample code for java.io.PrintWriter(java.lang.String, java.lang.String) definition code for java.io.PrintWriter(java.lang.String, java.lang.String)

public PrintWriter(String sample code for java.lang.String definition code for java.lang.String  fileName,
                   String sample code for java.lang.String definition code for java.lang.String  csn)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException ,
                   UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the provided charset.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(fileName) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5

PrintWriter sample code for java.io.PrintWriter(java.io.File) definition code for java.io.PrintWriter(java.io.File)

public PrintWriter(File sample code for java.io.File definition code for java.io.File  file)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException 
Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the default charset sample code for java.nio.charset.Charset.defaultCharset definition code for java.nio.charset.Charset.defaultCharset for this instance of the Java virtual machine.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(file.getPath()) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
Since:
1.5

PrintWriter sample code for java.io.PrintWriter(java.io.File, java.lang.String) definition code for java.io.PrintWriter(java.io.File, java.lang.String)

public PrintWriter(File sample code for java.io.File definition code for java.io.File  file,
                   String sample code for java.lang.String definition code for java.lang.String  csn)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException ,
                   UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Creates a new PrintWriter, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the provided charset.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(file.getPath()) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5
Method Detail

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

public void flush()
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
See Also:
checkError() sample code for java.io.PrintWriter.checkError() definition code for java.io.PrintWriter.checkError()

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

public void close()
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
See Also:
checkError() sample code for java.io.PrintWriter.checkError() definition code for java.io.PrintWriter.checkError()

checkError sample code for java.io.PrintWriter.checkError() definition code for java.io.PrintWriter.checkError()

public boolean checkError()
Flush the stream if it's not closed and check its error state. Errors are cumulative; once the stream encounters an error, this routine will return true on all successive calls.

Returns:
True if the print stream has encountered an error, either on the underlying output stream or during a format conversion.

setError sample code for java.io.PrintWriter.setError() definition code for java.io.PrintWriter.setError()

protected void setError()
Indicate that an error has occurred.


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

public void write(int c)
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.
<