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 = "Lazyroar " ; // 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( "Lazyroar" ); 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.533Lazyroar java.io.PrintWriter@1540e19d Geek false This is my Lazyroar program
Next Article: Java.io.PrintWriter class in Java | Set 2 This article is contributed by Nishant Sharma. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar 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.