Results 1 to 4 of 4
  1. #1
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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?

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

    Default Re: Nested constructors: why do they work?

    Quote Originally Posted by Daimoth View Post
    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?
    You piece of code could've been written as:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default Re: Nested constructors: why do they work?

    Quote Originally Posted by Daimoth View Post
    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 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  4. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  5. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM

Tags for this Thread

Posting Permissions

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