Results 1 to 4 of 4
- 11-17-2011, 09:19 AM #1
Nested constructors: why do they work?
I'm slowly learning about programs that write save files and read them. One particularly nasty concept was connecting a character stream to an output file. The example I found looks like this:
Java Code:File file = new File("output.txt"); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file) ), true)
But I don't understand why this doesn't produce errors. What I currently understand is that a character stream sent to System.out has to pass through each method before making its way onto output.txt
I also read this technique is called wrapping?
I don't, however, understand why "new BufferedWriter()" and "new FileWriter()" work by themselves inside those parameters. They both look like half a declaration to me. Does the "out" variable get assigned to one complicated object, or does it, in effect, create several?
- 11-17-2011, 09:31 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
Re: Nested constructors: why do they work?
You piece of code could've been written as:
... and forget about variables fw and bw. Those 'wrappers' (or 'decorators') each add some functionality to the object it wraps. I wrote a blog entry in this forum once that discusses those wrappers. (see the top of this page for the blog section).Java Code:File file = new File("output.txt") FileWriter fw= new FileWriter(file); BufferedWriter bw= new BufferedWriter(fw); PrintWriter out= new PrintWriter(bw, true);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-18-2011, 01:49 AM #3
Re: Nested constructors: why do they work?
I found it, but I'll have to bookmark the article for later, a few lines of your code are beyond me. Not for long, though. :p
Speaking of which, while searching for your blog I found this one, which is related; I'd been trying to figure out buffering.
I gleaned that character streams will request disc access often, whereas a buffered stream will only send chunks of data, mitigating the number of disc access requests, and therefore overhead. Does that sound right?
Last edited by Daimoth; 11-18-2011 at 02:09 AM. Reason: I've = I'd
- 11-18-2011, 08:39 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
Re: Nested constructors: why do they work?
You can always post a comment there if you want; if some of my code is unclear, I'd be happy to receive comments on it and I happily change the code where needed ...
Yup, right; when you hit the OS, you end up in a queue (waiting to be serviced) and when the OS has to hit the hard disk, things become even slower. Better read a lot of bytes from the hard disk in one go and don't go through all that hoopla for every single byte.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
applet call dll work in Win2000 but not work in WinXP
By manhcuongtin4 in forum Java AppletsReplies: 1Last Post: 07-14-2011, 01:45 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks