Menu
Topics Index
...
`

PrintWriterDemo - Java Example Program


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.

© meritcampus 2019

All Rights Reserved.

Open In App