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:
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?
Re: Nested constructors: why do they work?
Quote:
Originally Posted by
Daimoth
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:
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?
You piece of code could've been written as:
Code:
File file = new File("output.txt")
FileWriter fw= new FileWriter(file);
BufferedWriter bw= new BufferedWriter(fw);
PrintWriter out= new PrintWriter(bw, true);
... 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).
kind regards,
Jos
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?
:^):
Re: Nested constructors: why do they work?
Quote:
Originally Posted by
Daimoth
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
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 ...
Quote:
Originally Posted by
Daimoth
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?
:^):
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,
Jos