Class: java.io.PrintWriter
- public class PrintWriter
- extends Writer
Unlike the 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().
Methods
-
PrintWritertop
public PrintWriter(File file) throws FileNotFoundExceptionCreates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine. -
PrintWritertop
public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingExceptionCreates a new PrintWriter, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset. -
PrintWritertop
public PrintWriter(OutputStream out)Creates 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. -
PrintWritertop
public PrintWriter(OutputStream out, boolean autoFlush)Creates 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. -
PrintWritertop
public PrintWriter(Writer out)Creates a new PrintWriter, without automatic line flushing. -
PrintWritertop
public PrintWriter(Writer out, boolean autoFlush)Creates a new PrintWriter. -
PrintWritertop
public PrintWriter(String fileName) throws FileNotFoundExceptionCreates a new PrintWriter, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine. -
PrintWritertop
public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingExceptionCreates a new PrintWriter, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset. -
appendtop
public PrintWriter append(char c)Appends the specified character to this writer.An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation
out.write(c)- Specified by:
- append from Appendable
-
appendtop
public PrintWriter append(CharSequence csq)Appends the specified character sequence to this writer.An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation
out.write(csq.toString())Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.
- Specified by:
- append from Appendable
-
appendtop
public PrintWriter append(CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this writer.An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation
out.write(csq.subSequence(start, end).toString())- Specified by:
- append from Appendable
-
checkErrortop
public boolean checkError()Flushes the stream if it's not closed and checks its error state. -
clearErrortop
protected void clearError()Clears the error state of this stream.This method will cause subsequent invocations of java.io.PrintWriter.checkError() to return false until another write operation fails and invokes java.io.PrintWriter.setError().
-
closetop
public void close()Closes the stream and releases any system resources associated with it. Closing a previously closed stream has no effect. -
flushtop
public void flush()Flushes the stream. -
formattop
Writes a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.The locale always used is the one returned by Locale.getDefault(), regardless of any previous invocations of other formatting methods on this object.
-
formattop
Writes a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer. -
printtop
public void print(char c)Prints a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(double d)Prints a double-precision floating-point number. The string produced byString.valueOf(double)is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(float f)Prints a floating-point number. The string produced byString.valueOf(float)is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(int i)Prints an integer. The string produced byString.valueOf(int)is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(long l)Prints a long integer. The string produced byString.valueOf(long)is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(Object obj)Prints an object. The string produced by theString.valueOf(java.lang.Object)method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(String s)Prints a string. If the argument isnullthen the string"null"is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(boolean b)Prints a boolean value. The string produced byString.valueOf(boolean)is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printtop
public void print(char[] s)Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thejava.io.PrintWriter.write(int)method. -
printftop
A convenience method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation
out.format(format, args) -
printftop
A convenience method to write a formatted string to this writer using the specified format string and arguments. If automatic flushing is enabled, calls to this method will flush the output buffer.An invocation of this method of the form out.printf(l, format, args) behaves in exactly the same way as the invocation
out.format(l, format, args) -
printlntop
public void println()Terminates the current line by writing the line separator string. The line separator string is defined by the system propertyline.separator, and is not necessarily a single newline character ('\n'). -
printlntop
public void println(char x)Prints a character and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(char)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(double x)Prints a double-precision floating-point number and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(double)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(float x)Prints a floating-point number and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(float)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(int x)Prints an integer and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(int)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(long x)Prints a long integer and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(long)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(Object x)Prints an Object and then terminates the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokesjava.io.PrintWriter.print(java.lang.String)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(String x)Prints a String and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(java.lang.String)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(boolean x)Prints a boolean value and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(boolean)and thenjava.io.PrintWriter.println(). -
printlntop
public void println(char[] x)Prints an array of characters and then terminates the line. This method behaves as though it invokesjava.io.PrintWriter.print(char[])and thenjava.io.PrintWriter.println(). -
setErrortop
protected void setError()Indicates that an error has occurred.This method will cause subsequent invocations of java.io.PrintWriter.checkError() to return true until java.io.PrintWriter.clearError() is invoked.
-
writetop
public void write(int c)Writes a single character. -
writetop
public void write(String s)Writes a string. This method cannot be inherited from the Writer class because it must suppress I/O exceptions. -
writetop
public void write(String s, int off, int len)Writes a portion of a string. -
writetop
public void write(char[] buf)Writes an array of characters. This method cannot be inherited from the Writer class because it must suppress I/O exceptions. -
writetop
public void write(char[] buf, int off, int len)Writes A Portion of an array of characters.
Fields
-
out
protected Writer outThe underlying character-output stream of thisPrintWriter.
