Click here to Skip to main content
14,931,892 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class i4
    public static void main(String[] args) 
    PrintStream  z =new PrintStream (System.out) ;    
   z.print ( " this is print stream ") ;  
    z.println ( " hi") ;
   PrintWriter c = new PrintWriter (System.out , true) ;
   c.println ( " zxc ") ;
   c.print ( " hello  ") ;    }

When using PrintStream both the methods print the strings but for PrintWriter print() method does not print hello . Why ?

What I have tried:

I have tried this program on netbeans .
Updated 23-Apr-17 6:35am

1 solution

Look at the documentation: PrintWriter (Java Platform SE 7 )[^]
It says very clearly:
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.

So if you don't use println, nothing will happen to the output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900