• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IO Declaration

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, What is the difference between Line # 3 and 4. In what case, one statement is better than the other statement.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Buffering happens at a different call level in each case. That is about it.

At line 3, calls to the buffered writer do not go through to the other decorated classes until a threshold is reached and the stream is flushed.

At line 4, all calls to the PrintWriter will result in a call to the BufferedWriter. But the BufferedWriter can choose when it decides to flush its stream to the FileWriter.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain in a bit detail.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi bala, it would be better if you can please explain in detail. I am not clear either.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Detailed explanation would be really helpful.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that Deepak explained it rather well. What is it that you don't get?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. In what case, one statement is better than the other statement.
2. What is the meaning of "other decorated classes until a threshold is reached" at Line 3 comments
3. From line 3, new BufferedWriter(new PrintWriter(new FileWriter(new File("hello.txt")))); and
From line 4, new BufferedWriter(new FileWriter(new File("hello.txt"))); statements appears pretty much similar to me.

Wouter Oet wrote:I think that Deepak explained it rather well. What is it that you don't get?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

IMHO, line #3 is of little value. The reason you want to wrap something with a print writer, is so that you can have an api that allows you a lot of options on what to write -- ints, floats, strings, etc. etc.

If you then wrap that print writer with a buffered writer, don't you lose that rich api? What is the purpose of adding a richer interface, only to cover it up?

Henry
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like the direction Henry is headed...

What if you made a two column table of the methods provided by BufferedWriter and PrintWriter...

What are the differences?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the costly overloaded write() methods of these 2 classes.

BufferedWriter
---------------

write(char[] cbuf, int off, int len)
write(int c)
write(String s, int off, int len)

PrintWriter
------------

write(char[] buf)
write(char[] buf, int off, int len)
write(int c)
write(String s)
write(String s, int off, int len)...More other methods in this class than BuffredWriter

The extra 2 methods in PrintWriter gives high priority to wrap BufferedWriter with Printwriter/Writer classes ? Please advice

Also, Please let me know if you mean to say BufferedWriter wrapped with PrintWriter is safer/faster compared with PrintWriter wrapped with BufferedWriter ?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
say more about those extra methods...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bert Bates wrote:say more about those extra methods...



Personally, when I use a print writer, I tend to more likely use the 19 other print(), println(), and printf() methods. Maybe looking into those may be a good idea.

Henry
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me put in this way. Are you saying that, Because we have print methods in PrintWriter which are not there in BufferedWriter, we can wrap BufferedWriter with PrintWriter and use PrintWriter methods ? If so, I am fine. Performance wise, which one is better than the other in terms of write(). Both of these classes have some common overloaded write() methods.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harikrishna,

As far as the exam goes, the IO objective is all about functionality, not performance. So those extra, highly capable methods are what we're interested in, not which class might perform better. Now, once you get to the real world you might have cases where performance is important, but in general you'd want to start by leveraging the existing API as much as possible.

Does that make sense?

Bert
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert, I agree with you. Because, I am not clear what you guys are saying so far, I brought up performance issue. All I understand is Line 4 is better than Line 3..But not clear why and How..Please let me know if PrintWriter is better because it has more methods than BufferedWriter methods or some other reason.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Please let me know if PrintWriter is better because it has more methods than BufferedWriter methods or some other reason.


No.

For one thing there isn't an absolute idea of "better". There is only "better" in a certain context.

So for example if you wanted to use the methods which PrintWriter has but which BufferedWriter doesn't have, then PrintWriter would be better in that case. But if you didn't need those methods, then PrintWriter wouldn't be better in that case.

So whenever you see somebody saying (or asking whether) "A is better than B" you should bear in mind that the answer is always "It depends on the context". Of course you did say that in your original post:

In what case, one statement is better than the other statement.


but it looks like you might have lost sight of that.

Also there's this feature of PrintWriter (from the API documentation):

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().


You might decide it's inconvenient to have to call that method all the time to see if exceptions happened -- in some cases, but not in other cases. If it happens that it is inconvenient, then that makes PrintWriter less "better" than it would have been in those other cases.
 
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic