The Wayback Machine - https://web.archive.org/web/20240921152108/https://www.geeksforgeeks.org/java-io-printwriter-class-java-set-1/
Open In App

Java.io.PrintWriter class in Java | Set 1

Last Updated : 12 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Java PrintWriter 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(). 

Declaration of Java PrintWriter Class

public class PrintWriter extends Writer  

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 of PrinterWriter class in Java

Methods of PrinterWriter Class in Java are mentioned below:

Method Description
PrintWriter append(char c) Appends the specified character to this writer
PrintWriter append(CharSequence csq, int start, int end) Appends the specified character sequence to this writer.
PrintWriter append(CharSequence csq)  Appends a subsequence of the specified character sequence to this writer.
boolean checkError() Flushes the stream and checks its error state.
protected void clearError() Clears the internal error state of this stream.
void close() Closes the stream and releases any system resources associated with it.
void flush() Flushes the stream.
PrintWriter format(Locale l, String format, Object… args) Writes a formatted string to this writer using the specified format string and arguments.
PrintWriter format(String format, Object… args) Writes a formatted string to this writer using the specified format string and arguments.
void print(boolean b) Prints a boolean value.
void print(char c) Prints a character.
void print(char[] s) Prints a character array.
void print(double d) Prints a double.
void print(float f) Prints a float.
void print(int i) Prints an integer.
void print(long l) Prints a long integer.
void print(Object obj) Prints an object.
void print(String s) Prints a string.

1. PrintWriter append(char c)

Appends the specified character to this writer

Syntax :public PrintWriter append(char c)

Parameters:
c - The 16-bit character to append

Returns:
This writer

2. 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

3. 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

4. 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

5. protected void clearError()

Clears the internal error state of this stream.

Syntax :protected void clearError()

6. void close()

Closes the stream and releases any system resources associated with it.

Syntax :public void close()

Specified by:close in class Writer

7. void flush()

Flushes the stream.

Syntax :public void flush()

Specified by:flush in interface Flushable

Specified by:flush in class Writer

8. 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

9. 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 

10. void print(boolean b)

Prints a boolean value.

Syntax :public void print(boolean b)

11. void print(char c)

Prints a character.

Syntax :public void print(char c)

12. void print(char[] s)

Prints an array of characters.

Syntax :public void print(char[] s)

13. void print(double d)

Prints a double-precision floating-point number.

Syntax :public void print(double b)

14. void print(float f)

Prints a floating-point number.

Syntax :public void print(float f)

15. void print(int i)

Prints an integer.

Syntax :public void print(int i)

16. void print(long l)

Prints a long integer.

Syntax :public void print(long l)

17. void print(Object obj)

Prints an object.

Syntax :public void print(Object obj)

18. void print(String s)

Prints a string.

Syntax :public void print(String s)

Example

Java




// Java program to demonstrate PrintWriter
import java.io.*;
import java.util.Locale;
  
// Driver Class
class PrintWriterDemo {
    // Main Function
    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



Similar Reads

Java.io.PrintWriter class in Java | Set 2
Java.io.PrintWriter class in Java | Set 1 More methods: PrintWriter printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(Locale l, String format, Object... args) Parameters: l - The locale to apply during f
7 min read
PrintWriter flush() method in Java with Examples
The flush() method of PrintWriter Class in Java is used to flush the stream. By flushing the stream, it means to clear the stream of any element that may be or maybe not inside the stream. It neither accepts any parameter nor returns any value. Syntax: public void flush() Parameters: This method do not accepts any parameter. Return Value: This meth
2 min read
PrintWriter println(String) method in Java with Examples
The println(String) method of PrintWriter Class in Java is used to print the specified String on the stream and then break the line. This String is taken as a parameter. Syntax: public void println(String string) Parameters: This method accepts a mandatory parameter string which is the String to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println(Object) method in Java with Examples
The println(Object) method of PrintWriter Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter print(char[]) method in Java with Examples
The print(char[]) method of PrintWriter Class in Java is used to print the specified character array on the stream. This character array is taken as a parameter. Syntax: public void print(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be printed in the Stream. Return Value: This met
2 min read
PrintWriter print(Object) method in Java with Examples
The print(Object) method of PrintWriter Class in Java is used to print the specified Object on the stream. This Object is taken as a parameter. Syntax: public void print(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method do not returns any value. Bel
2 min read
PrintWriter print(String) method in Java with Examples
The print(String) method of PrintWriter Class in Java is used to print the specified String value on the stream. This String value is taken as a parameter. Syntax: public void print(String StringValue) Parameters: This method accepts a mandatory parameter StringValue which is the String value to be written on the stream. Return Value: This method d
2 min read
PrintWriter write(String, int, int) method in Java with Examples
The write(String, int, int) method of PrintWriter Class in Java is used to write a specified portion of the specified String on the stream. This String is taken as a parameter. The starting index and length of String to be written are also taken as parameters. Syntax: public void write(String string, int startingIndex, int lengthOfstring) Parameter
2 min read
PrintWriter write(String) method in Java with Examples
The write(String) method of PrintWriter Class in Java is used to write the specified String on the stream. This String value is taken as a parameter. Syntax: public void write(String string) Parameters: This method accepts a mandatory parameter string which is the String to be written in the Stream. Return Value: This method do not returns any valu
2 min read
PrintWriter write(char[]) method in Java with Examples
The write(char[]) method of PrintWriter Class in Java is used to write the specified character array on the stream. This character array is taken as a parameter. Syntax: public void write(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be written in the Stream. Return Value: This met
2 min read
PrintWriter write(char[], int, int) method in Java with Examples
The write(char[], int, int) method of PrintWriter Class in Java is used to write a specified portion of the specified character array on the stream. This character array is taken as a parameter. The starting index and length of characters to be written are also taken as parameters. Syntax: public void write(char[] charArray, int startingIndex, int
2 min read
PrintWriter write(int) method in Java with Examples
The write(int) method of PrintWriter Class in Java is used to write the specified character on the stream. This character is specified using the ASCII value of the character passed as an integer value. This integer value is taken as a parameter. Syntax: public void write(int ascii) Parameters: This method accepts a mandatory parameter ascii which i
2 min read
PrintWriter clearError() method in Java with Examples
The clearError() method of PrintWriter Class in Java is used to clear the error state of this PrintWriter instance. It clears any error that might have or not happened in the stream. Hence the checkError() method will always return false after this method. Syntax: protected void clearError() Parameters: This method do not accepts any parameter. Ret
2 min read
PrintWriter checkError() method in Java with Examples
The checkError() method of PrintWriter Class in Java is used to check the error state of this PrintWriter instance. This method flushes the stream in order to check the error state. It returns a boolean value that tells if the stream has encountered any error or not. Syntax: public boolean checkError() Parameters: This method do not accepts any par
2 min read
PrintWriter setError() method in Java with Examples
The setError() method of PrintWriter Class in Java is used to set the error state of this PrintWriter instance. This method is used as an indicator to indicate that an error has occurred in the stream. It is protected and hence needs to be implemented by deriving the PrintWriter class to use it. Syntax: protected void setError() Parameters: This me
2 min read
PrintWriter close() method in Java with Examples
The close() method of PrintWriter Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintWriter instance once closed won't work. Also a PrintWriter instance once closed cannot be closed again. Syntax: public void close() Parameters: This method do not accepts any paramet
2 min read
PrintWriter println() method in Java with Examples
The println() method of PrintWriter Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods illustrates the working of println() method: Program
2 min read
PrintWriter printf(Locale, String, Object) method in Java with Examples
The printf(Locale, String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(Locale locale, String format, Object...args) Parameters: This method accepts two mand
2 min read
PrintWriter println(char[]) method in Java with Examples
The println(char[]) method of PrintWriter Class in Java is used to print the specified character array on the stream and then break the line. This character array is taken as a parameter. Syntax: public void println(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be printed in the St
2 min read
PrintWriter printf(String, Object) method in Java with Examples
The printf(String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter printf(String format, Object...args) Parameters: This method accepts two mandatory parameter: format which is the format ac
2 min read
PrintWriter append(CharSequence) method in Java with Examples
The append(CharSequence) method of PrintWriter Class in Java is used to write the specified CharSequence on the stream. This CharSequence value is taken as a parameter. Syntax: public PrintWriter append(CharSequence charSequence) Parameters: This method accepts a mandatory parameter charSequence which is the CharSequence to be written in the Stream
2 min read
PrintWriter format(String, Object) method in Java with Examples
The format(String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter format(String format, Object...args) Parameters: This method accepts two mandatory parameter: format which is the format ac
2 min read
PrintWriter append(CharSequence, int, int) method in Java with Examples
The append(CharSequence, int, int) method of PrintWriter Class in Java is used to append a specified portion of the specified charSequence on the stream. This charSequence is taken as a parameter. The starting index and ending index to be written are also taken as parameters. Syntax: public void append(CharSequence charSequence, int startingIndex,
2 min read
PrintWriter format(Locale, String, Object) method in Java with Examples
The format(Locale, String, Object) method of PrintWriter Class in Java is used to print a formatted string in the stream using the given Locale. The string is formatted using specified format and arguments passed as the parameter. Syntax: public PrintWriter format(Locale locale, String format, Object...args) Parameters: This method accepts two mand
2 min read
Properties list(PrintWriter) method in Java with Examples
The list(PrintWriter) method of Properties class is used to print this Properties list out to the specified output stream, passed as the parameter. This method can be used for debugging purpose as it can help to see the Properties elements on the stream.0 Syntax: public void list(PrintWriter out) Parameters: This method accepts a parameters PrintWr
2 min read
PrintWriter print(float) method in Java with Examples
The print(float) method of PrintWriter Class in Java is used to print the specified float value on the stream. This float value is taken as a parameter. Syntax: public void print(float floatValue) Parameters: This method accepts a mandatory parameter floatValue which is the float value to be written on the stream. Return Value: This method do not r
2 min read
PrintWriter print(double) method in Java with Examples
The print(double) method of PrintWriter Class in Java is used to print the specified double value on the stream. This double value is taken as a parameter. Syntax:public void print(double doubleValue)Parameters: This method accepts a mandatory parameter doubleValue which is the double value to be written on the stream. Return Value: This method doe
2 min read
PrintWriter print(int) method in Java with Examples
The print(int) method of PrintWriter Class in Java is used to print the specified int value on the stream. This int value is taken as a parameter. Syntax:public void print(int intValue)Parameters: This method accepts a mandatory parameter intValue which is the int value to be written on the stream. Return Value: This method does not return any valu
2 min read
PrintWriter print(boolean) method in Java with Examples
The print(boolean) method of PrintWriter Class in Java is used to print the specified boolean value on the stream. This boolean value is taken as a parameter. Syntax: public void print(boolean booleanValue) Parameters: This method accepts a mandatory parameter booleanValue which is the boolean value to be written on the stream. Return Value: This m
2 min read
PrintWriter print(long) method in Java with Examples
The print(long) method of PrintWriter Class in Java is used to print the specified long value on the stream. This long value is taken as a parameter. This method is similar to write(String.valueOf(long)) where the String.valueOf(long) returns the number in bytes and these bytes are written on the Stream with the help of write() method. Syntax: publ
2 min read
Article Tags :
Practice Tags :