PrintWriter
PrintWriter is a character-oriented version of PrintStream. It implements the Appendable, Closeable, and Flushable interfaces.
PrintWriter has several constructors.
- PrintWriter(OutputStream outputStream)
PrintWriter(OutputStream outputStream, boolean flushOnNewline)
- outputStream specifies an open OutputStream that will receive output.
- PrintWriter(Writer outputStream)
PrintWriter(Writer outputStream, boolean flushOnNewline)
- flushOnNewline parameter controls whether the output buffer is flushed every time println( ), printf( ), or format( ) is called. Constructors that do not specify the flushOnNewline parameter do not automatically flush.
- PrintWriter(File outputFile) throws FileNotFoundException
- PrintWriter(File outputFile, String charSet) throws FileNotFoundException, UnsupportedEncodingException
- PrintWriter(String outputFileName) throws FileNotFoundException
PrintWriter(String outputFileName, String charSet) throws FileNotFoundException, UnsupportedEncodingException
- PrintWriter is created from a File. The file is created or any preexisting file is destroyed.
PrintWriter supports the print( ) and println( ) methods for all types, including Object. If an argument is not a primitive type, the PrintWriter methods will call the object's toString( ) method and then output the result.
PrintWriter supports the printf( ) method which accepts the precise format of the data.
PrintWriter printf(String fmtString, Object ... args)
PrintWriter printf(Locale loc, String fmtString, Object ... args)
The first version writes args to standard output in the format specified by fmtString, using the default locale.
PrintWriter append(char c)
- Appends the specified character to this writer.
PrintWriter append(CharSequence csq)
- Appends the specified character sequence to this writer.
PrintWriter append(CharSequence csq, int start, int end)
- Appends a subsequence of the specified character sequence to this writer.
boolean checkError()
- Flushes the stream if it's not closed and checks its error state.
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 an array of characters.
void print(double d)
- Prints a double-precision floating-point number.
void print(float f)
- Prints a floating-point number.
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.
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.
PrintWriter printf(String format, Object... args)
- A convenience method to write a formatted string to this writer using the specified format string and arguments.
void println()
- Terminates the current line by writing the line separator string.
void println(boolean x)
- Prints a boolean value and then terminates the line.
void println(char x)
- Prints a character and then terminates the line.
void println(char[] x)
- Prints an array of characters and then terminates the line.
void println(double x)
- Prints a double-precision floating-point number and then terminates the line.
void println(float x)
- Prints a floating-point number and then terminates the line.
void println(int x)
- Prints an integer and then terminates the line.
void println(long x)
- Prints a long integer and then terminates the line.
void println(Object x)
- Prints an Object and then terminates the line.
void println(String x)
- Prints a String and then terminates the line.
void write(char[] buf)
- Writes an array of characters.
void write(char[] buf, int off, int len)
- Writes A Portion of an array of characters.
void write(int c)
- Writes a single character.
void write(String s)
- Writes a string.
void write(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