PrintWriter class

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

Prints formatted representations of objects to a text-output stream.

ConstructorSummary
PrintWriter(File file)Creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter(File file, String csn)Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(OutputStream out)Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter(OutputStream out, boolean autoFlush)Creates a new PrintWriter from an existing OutputStream.
PrintWriter(String fileName)Creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter(String fileName, String csn)Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter(Writer out)Creates a new PrintWriter, without automatic line flushing.
PrintWriter(Writer out, boolean autoFlush)Creates a new PrintWriter.

ReturnMethodSummary
PrintWriterappend(char c)Appends the specified character to this writer.
PrintWriterappend(CharSequence csq)Appends the specified character sequence to this writer.
PrintWriterappend(CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this writer.
booleancheckError()Flushes the stream if it's not closed and checks its error state.
voidclose()Closes the stream and releases any system resources associated with it.
voidflush()Flushes the stream.
PrintWriterformat(Locale l, String format, Object... args)Writes a formatted string to this writer using the specified format string and arguments.
PrintWriterformat(String format, Object... args)Writes a formatted string to this writer using the specified format string and arguments.
voidprint(boolean b)Prints a boolean value.
voidprint(char c)Prints a character.
voidprint(char[] s)Prints an array of characters.
voidprint(double d)Prints a double-precision floating-point number.
voidprint(float f)Prints a floating-point number.
voidprint(int i)Prints an integer.
voidprint(long l)Prints a long integer.
voidprint(Object obj)Prints an object.
voidprint(String s)Prints a string.
PrintWriterprintf(Locale l, String format, Object... args)A convenience method to write a formatted string to this writer using the specified format string and arguments.
PrintWriterprintf(String format, Object... args)A convenience method to write a formatted string to this writer using the specified format string and arguments.
voidprintln()Terminates the current line by writing the line separator string.
voidprintln(boolean x)Prints a boolean value and then terminates the line.
voidprintln(char x)Prints a character and then terminates the line.
voidprintln(char[] x)Prints an array of characters and then terminates the line.
voidprintln(double x)Prints a double-precision floating-point number and then terminates the line.
voidprintln(float x)Prints a floating-point number and then terminates the line.
voidprintln(int x)Prints an integer and then terminates the line.
voidprintln(long x)Prints a long integer and then terminates the line.
voidprintln(Object x)Prints an Object and then terminates the line.
voidprintln(String x)Prints a String and then terminates the line.
voidwrite(char[] buf)Writes an array of characters.
voidwrite(char[] buf, int off, int len)Writes A Portion of an array of characters.
voidwrite(int c)Writes a single character.
voidwrite(String s)Writes a string.
voidwrite(String s, int off, int len)Writes a portion of a string.
Revised from Open JDK source code

Create PrintWriter from System.out


import java.io.PrintWriter;

public class Main {

  public static void main(String args[]) throws Exception {

    PrintWriter pw = new PrintWriter(System.out);

    pw.println(true);
    pw.println('A');
    pw.println(500);
    pw.println(40000L);
    pw.println(45.67f);
    pw.println(45.67);
    pw.println("Hello");
    pw.println(new Integer("99"));

    pw.close();
  }
}

The output:


true
A
500
40000
45.67
45.67
Hello
99
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.