• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to the console with PrintWriter  RSS feed

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you can use PrintWriter to write to the console but when I run the following code I only get "abc" printed to the console. Any help would be appreciated!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I looked up the documentation for PrintWriter it said something about implements Flushable. I suggest you try flushing the PrintWriter and see what happens.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought, we need to flush to avoid problem with subsequent operations.

So if we just have a single statement of printwriter.println() - without flushing - we shouldn't face any problem, right?
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The flush() did it, thanks!
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:The flush() did it, thanks!


You can enable flush on constructor too,

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, what happens is that the writer “stores” all its output in a sort of buffer. That usually improves performance; rather than writing one line at a time you can write several lines, when the buffer is full. Remember that console output and writing to a file are slow operations (a few milliseconds) compared to filling the buffer (probably thousands of characters in a μsecond). If the buffer is not full, you need to flush it. In the case of a file writer, that is usually easy; you have to close it and the close() method usually calls flush() first.

In the case of a writer pointing to System.out, however, you must never close it.

Try writing writer.flush(); only once, after line 7 in that little class, or writer.close() after line 6, and see what happens in both cases.

By the way: Class1 is a very poor name for a class.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, what happens is that the writer “stores” all its output in a sort of buffer. That usually improves performance; rather than writing one line at a time you can write several lines, when the buffer is full. Remember that console output and writing to a file are slow operations (a few milliseconds) compared to filling the buffer (probably thousands of characters in a μsecond). If the buffer is not full, you need to flush it. In the case of a file writer, that is usually easy; you have to close it and the close() method usually calls flush() first.

In the case of a writer pointing to System.out, however, you must never close it.

Try writing writer.flush(); only once, after line 7 in that little class, or writer.close() after line 6, and see what happens in both cases.

By the way: Class1 is a very poor name for a class.


So to get the benefit of the buffer you should only flush at the end of a series of outputs when you need the data to be there, correct? Class1 is a file I used to write 100's of code snippets while studying for my Level I test. I agree it would be a bad name for a real program, but it is convenient to use to test a small part of a real program which is what I did here.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree about flushing only when you need the data. I presume that the buffer will flush itself if it gets full, but have never done any tests to verify that.
Take your point about ”Class1”
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are writing to a file you will have to close the writer anyway; that usually flushes the buffer. if you don't close the writer not only might you suffer a resource leak, but you might also fail to write anything to your file. Most people use try‑with‑resources to close readers and writers.
What happened when you closed the writer pointing to System.out?
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't close it because I was writing to System.out which I believe means I couldn't re-open it later in the program. That would also be a problem with the try-with-resources. I tried using the try-with-resources with a Scanner for user input but then I couldn't use the method again because the stream was closed and couldn't be reopened.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I tried closing that Writer last night and the System.out calls simply didn't work. The same applies to anything reading from System.in (e.g. a Scanner).

Create a utility class with your Scanner a private static final field, then you can use methods which use that Scanner and you never need to close it. No need for try with resources.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!