Java.io.PrintWriter class in Java | Set 1 - GeeksforGeeks

24 Jan, 2017
This class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.Unlike the 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().Constructor and Description
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.
Methods:
PrintWriter append(char c) : Appends the specified character to this writerSyntax :public PrintWriter append(char c)
Parameters:
c - The 16-bit character to append
Returns:
This writer
Syntax :public PrintWriter append(char c)
Parameters:
c - The 16-bit character to append
Returns:
This writer
PrintWriter append(CharSequence csq, int start, int end): Appends the specified character sequence to this writer.Syntax :public PrintWriter append(CharSequence csq,
        int start,
        int end)
Parameters:
        csq - The character sequence from which a subsequence will be appended.
        start - The index of the first character in the subsequence
        end - The index of the character following the last character in the subsequence
Returns:This writer
Throws:
        IndexOutOfBoundsException
Syntax :public PrintWriter append(CharSequence csq,
        int start,
        int end)
Parameters:
        csq - The character sequence from which a subsequence will be appended.
        start - The index of the first character in the subsequence
        end - The index of the character following the last character in the subsequence
Returns:This writer
Throws:
        IndexOutOfBoundsException
PrintWriter append(CharSequence csq) : Appends a subsequence of the specified character sequence to this writer.Syntax :public PrintWriter append(CharSequence csq)
Parameters:
        csq - The character sequence to append.
Returns: This writer

Syntax :public PrintWriter append(CharSequence csq)
Parameters:
        csq - The character sequence to append.
Returns: This writer

boolean checkError(): Flushes the stream and checks its error state.Syntax :public boolean checkError()
Returns: true if and only if this stream 
has encountered an IOException other than InterruptedIOException, 
or the setError method has been invoked
Syntax :public boolean checkError()
Returns: true if and only if this stream 
has encountered an IOException other than InterruptedIOException, 
or the setError method has been invoked
protected void clearError() : Clears the internal error state of this stream.Syntax :protected void clearError()

Syntax :protected void clearError()

void close() : Closes the stream and releases any system resources associated with it.Syntax :public void close()
Specified by:close in class Writer
Syntax :public void close()
Specified by:close in class Writer
void flush(): Flushes the stream.Syntax :public void flush()
Specified by:flush in interface Flushable
Specified by:flush in class Writer

Syntax :public void flush()
Specified by:flush in interface Flushable
Specified by:flush in class Writer

PrintWriter format(Locale l, String format, Object... args): Writes a formatted string to this writer using the specified format string and arguments.Syntax :public PrintWriter format(Locale l,
        String format,
        Object... args)
Parameters:
        l - The locale to apply during formatting. If l is null,
 then no localization is applied.
        format - A format string as described in Format string syntax
        args - Arguments referenced by the format specifiers in the format string. 
The number of arguments is variable and may be zero.
Returns: This writer
Throws:
        IllegalFormatException
        NullPointerException
Syntax :public PrintWriter format(Locale l,
        String format,
        Object... args)
Parameters:
        l - The locale to apply during formatting. If l is null,
 then no localization is applied.
        format - A format string as described in Format string syntax
        args - Arguments referenced by the format specifiers in the format string. 
The number of arguments is variable and may be zero.
Returns: This writer
Throws:
        IllegalFormatException
        NullPointerException
PrintWriter format(String format, Object... args): Writes a formatted string to this writer using the specified format string and arguments.Syntax :public PrintWriter format(String format,
        Object... args)
Parameters:
        format - A format string as described in Format string syntax
        args - Arguments referenced by the format specifiers in the format string. 
The number of arguments is variable and may be zero.
Returns: This writer
Throws:
        IllegalFormatException
        NullPointerException 
Syntax :public PrintWriter format(String format,
        Object... args)
Parameters:
        format - A format string as described in Format string syntax
        args - Arguments referenced by the format specifiers in the format string. 
The number of arguments is variable and may be zero.
Returns: This writer
Throws:
        IllegalFormatException
        NullPointerException 
void print(boolean b): Prints a boolean value.Syntax :public void print(boolean b)
Syntax :public void print(boolean b)
void print(char c): Prints a character.Syntax :public void print(char c)
Syntax :public void print(char c)
void print(char[] s): Prints an array of characters.Syntax :public void print(char[] s)

Syntax :public void print(char[] s)

void print(double d) :Prints a double-precision floating-point number.Syntax :public void print(double b)

Syntax :public void print(double b)

void print(float f): Prints a floating-point number.Syntax :public void print(float f)
Syntax :public void print(float f)
void print(int i): Prints an integer.Syntax :public void print(int i)
Syntax :public void print(int i)
void print(long l): Prints a long integer.Syntax :public void print(long l)
Syntax :public void print(long l)
void print(Object obj) :Prints an object.Syntax :public void print(Object obj)
Syntax :public void print(Object obj)
void print(String s): Prints a string.Syntax :public void print(String s)
Syntax :public void print(String s)
Program:
import java.io.*;import java.util.Locale;//Java program to demonstrate PrintWriterclass PrintWriterDemo {      public static void main(String[] args)         {        String s="GeeksforGeeks";          // create a new writer        PrintWriter out = new PrintWriter(System.out);        char c[]={'G','E','E','K'};                  //illustrating print(boolean b) method        out.print(true);                  //illustrating print(int i) method        out.print(1);                  //illustrating print(float f) method        out.print(4.533f);                  //illustrating print(String s) method        out.print("GeeksforGeeks");        out.println();                  //illustrating print(Object Obj) method        out.print(out);        out.println();                  //illustrating append(CharSequence csq) method        out.append("Geek");        out.println();                  //illustrating checkError() method        out.println(out.checkError());                  //illustrating format() method        out.format(Locale.UK, "This is my %s program", s);                  //illustrating flush method        out.flush();                  //illustrating close method        out.close();    }}
Output:
        true14.533GeeksforGeeks
        java.io.PrintWriter@1540e19d
        Geek
        false
        This is my GeeksforGeeks program
Next Article: Java.io.PrintWriter class in Java | Set 2
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-I/O
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java
LinkedList in Java