Results 1 to 7 of 7

Thread: Print Writer

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Pakistan
    Posts
    273
    Rep Power
    0

    Default Print Writer

    Java Code:
    	public static void main(String[] args) throws IOException {
    
    		InputStreamReader input = new InputStreamReader(System.in);
    		PrintWriter out = new PrintWriter(System.out, true);
    		out.print("Enter String : "); // doesnot show this without pressing
    										// enter
    		BufferedReader buffer = new BufferedReader(input);
    		String s1 = buffer.readLine();
    		out.println(s1.trim());
    
    	}
    It does not print "Enter String" before I press enter... whats wrong with this ???

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Print Writer

    Do out.flush() if you want to see all of the output produced by the 'out' object.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Print Writer

    You set the autoFlush flag (that's the 'true' in the constructor) which means it only flushes to wherever it is writing when a println or equivalent is called.
    You don't call println.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Pakistan
    Posts
    273
    Rep Power
    0

    Default Re: Print Writer

    but i called print ??

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Pakistan
    Posts
    273
    Rep Power
    0

    Default Re: Print Writer

    Its working for println and not for print ... it works with print if I set out.flush() ... whats the difference ????

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Print Writer

    Quote Originally Posted by allaudin View Post
    Its working for println and not for print ... it works with print if I set out.flush() ... whats the difference ????
    As always the (definite) answer is in the API documentation; the Writer you're using is line buffered; i.e. every character is buffered until the buffer is full or until a new line character is printed.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Pakistan
    Posts
    273
    Rep Power
    0

Similar Threads

  1. Java and Writer
    By wing in forum New To Java
    Replies: 11
    Last Post: 08-19-2011, 10:27 AM
  2. Encoding in java.io.writer
    By hariharabalan in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 11:27 AM
  3. CSV file writer
    By nida in forum Java Servlet
    Replies: 3
    Last Post: 05-08-2009, 02:08 PM
  4. Replies: 6
    Last Post: 04-09-2009, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •