Creating a PrintWriter with BufferedWriter versus just File

Discussion in 'Java' started by KevinSimonson, Sep 30, 2011.

  1. What's the difference between this code:

    PrintWriter destination
    = new PrintWriter
    ( new BufferedWriter( new FileWriter( new File( arg))));

    and this code:

    PrintWriter destination = new PrintWriter( new File( arg));

    Do I get buffered output with the first code that I don't get with the
    second code? Or do I get the same buffered behavior with both?

    Kevin Simonson
     
    KevinSimonson, Sep 30, 2011
    #1
    1. Advertising

  2. KevinSimonson

    Lew Guest

    KevinSimonson wrote:
    > What's the difference between this code:
    >
    > PrintWriter destination
    > = new PrintWriter
    > ( new BufferedWriter( new FileWriter( new File( arg))));
    >
    > and this code:
    >
    > PrintWriter destination = new PrintWriter( new File( arg));
    >
    > Do I get buffered output with the first code that I don't get with the
    > second code? Or do I get the same buffered behavior with both?


    No, the write semantics differ between the two. In the BufferedWriter version, writes to the underlying stream occur only when the BufferedWriter buffer fills. With the second form, writes occur every time the PrintWriter writes. There is nothing in the documentation of PrintWriter
    <http://download.oracle.com/javase/7/docs/api/java/io/PrintWriter.html>
    to indicate that it buffers writes. So it seems unlikely that you would "get the same buffered behavior with both".

    --
    Lew
     
    Lew, Sep 30, 2011
    #2
    1. Advertising

  3. KevinSimonson

    Arne Vajhøj Guest

    On 9/30/2011 1:00 PM, KevinSimonson wrote:
    > What's the difference between this code:
    >
    > PrintWriter destination
    > = new PrintWriter
    > ( new BufferedWriter( new FileWriter( new File( arg))));
    >
    > and this code:
    >
    > PrintWriter destination = new PrintWriter( new File( arg));
    >
    > Do I get buffered output with the first code that I don't get with the
    > second code? Or do I get the same buffered behavior with both?


    You can lookup the classes in the Java docs.

    If the docs does not say that class XYZ does ABC, then
    you should not rely on class XYZ doing ABC even though
    a specific implementation may do it.

    Arne
     
    Arne Vajhøj, Oct 1, 2011
    #3
  4. KevinSimonson

    Roedy Green Guest

    On Fri, 30 Sep 2011 10:00:55 -0700 (PDT), KevinSimonson
    <> wrote, quoted or indirectly quoted someone who
    said :

    >What's the difference between this code:
    >
    >PrintWriter destination
    > = new PrintWriter
    > ( new BufferedWriter( new FileWriter( new File( arg))));
    >
    >and this code:
    >
    >PrintWriter destination = new PrintWriter( new File( arg));
    >
    >Do I get buffered output with the first code that I don't get with the
    >second code? Or do I get the same buffered behavior with both?


    You don't get buffering, or at least significant buffering. Given that
    computers have at least four times as much RAM as they had when Java
    io was conceived, you probably want to use 64K buffers, or read the
    whole file in one fell swoop, when the file does not stay open.
    see http://mindprod.com/products1.html#HUNKIO

    See http://mindprod.com/applet/fileio.html
    to generate you sample code for Java I/O.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    It should not be considered an error when the user starts something
    already started or stops something already stopped. This applies
    to browsers, services, editors... It is inexcusable to
    punish the user by requiring some elaborate sequence to atone,
    e.g. open the task editor, find and kill some processes.
     
    Roedy Green, Oct 1, 2011
    #4
  5. KevinSimonson

    Arne Vajhøj Guest

    On 10/1/2011 8:06 AM, Roedy Green wrote:
    > On Fri, 30 Sep 2011 10:00:55 -0700 (PDT), KevinSimonson
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >> What's the difference between this code:
    >>
    >> PrintWriter destination
    >> = new PrintWriter
    >> ( new BufferedWriter( new FileWriter( new File( arg))));
    >>
    >> and this code:
    >>
    >> PrintWriter destination = new PrintWriter( new File( arg));
    >>
    >> Do I get buffered output with the first code that I don't get with the
    >> second code? Or do I get the same buffered behavior with both?

    >
    > You don't get buffering, or at least significant buffering. Given that
    > computers have at least four times as much RAM as they had when Java
    > io was conceived,


    4??

    More than 40!

    > you probably want to use 64K buffers,


    64 KB buffer was not a problem when java.io was invented.

    Arne
     
    Arne Vajhøj, Oct 1, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Novice
    Replies:
    1
    Views:
    1,153
    Mike Schilling
    Jul 19, 2003
  2. Brett Everton
    Replies:
    2
    Views:
    3,328
    Brett Everton
    Jun 4, 2004
  3. Jean-Benoit MORLA
    Replies:
    3
    Views:
    9,764
    Jean-Benoit MORLA
    Jul 19, 2004
  4. Jan Burse
    Replies:
    3
    Views:
    339
    Tom Anderson
    Jan 8, 2012
  5. Paul Butcher
    Replies:
    12
    Views:
    723
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page