Menu
Topics Index
...
`


Input/Output: Exploring java.io > Character Streams >
Siva Nookala - 20 Feb 2017
PrintWriter is a subclass of Java Writer Class. It is used to print formatted representations of objects to a text-output stream. This class implements all of the print methods in Java PrintStream Class . It does not contain methods for writing raw bytes, for writing raw bytes we should use byte streams.

public class PrintWriter
extends Writer
Unlike Java PrintStream Class automatic flushing is enabled only when one of the println, printf, or format methods is invoked.

Methods in this class never throw I/O exceptions, although some of its constructors may.
Frequently Used Constructors :
Constructors Description
PrintWriter(File file) Creates a new PrintWriter, without automatic line flushing, with the specified file.
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.
Methods :
Method Description
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.
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.
protected void    setError() Indicates that an error has occurred.
boolean checkError() Flushes the stream if it's not closed and checks its error state.
protected void clearError() Clears the error state of this 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.
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 flush() Flushes the stream.
void close() Closes the stream and releases any system resources associated with it.
PrintWriterDemo
import java.util.*;

class PrintWriterDemo
{
    public static void main(String arg[])
    {
        String s = " I am Alone";
        char c = ',';
        PrintWriter pw = new PrintWriter(System.out); // LINE A
        pw.format("%s%c But happy",s,c); // LINE B
        pw.append('.'); // LINE C
        pw.println();
        pw.println(s); // LINE D
        pw.close(); // LINE E    
    }
}
OUTPUT

I am Alone, But happy.
I am Alone

DESCRIPTION

At LINE A we created PrintWriter for OutputStream System. System.out writes data to console.
At LINE B we are formating the Stream we are adding a character and String.
At LINE C we are adding a character to the Stream.
At LINE D we are printing the String s.
At LINE E we are closing the Stream.

THINGS TO TRY
  • Now Create PrintWriter object for OutputStream System as shown in the program.
  • Write "Merit Campus" to the PrintWriter using write method.
  • Using format method format the PrintWriter as "Merit Campus My online Java School".
  • Comment LINE E and run the program. No compilation error but you will not get output since the Stream is not yet closed.
More Constructors :
Constructors Description
PrintWriter(File file, String csn) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
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.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App