Close and BufferWriter

Discussion in 'Java' started by Berlin Brown, Sep 7, 2004.

  1. Berlin  Brown

    Berlin Brown Guest

    This is basic java, and I have an idea, but without looking at the java
    code, I cant get an 100% understanding.

    File _file = new File("somefilename");
    FileWriter fileWriter = null;
    BufferedWriter outputFile = null

    fileWriter = new FileWriter(_file);
    outputFile = new BufferedWriter(fileWriter);
    PrintWriter out = new PrintWriter(outputFile);

    outputFile.close(); // ???
    out.close(); // ???


    In this close what would physically close the file.
    outputFile.close() ...which is the BufferedWriter or
    out.close();
     
    Berlin Brown, Sep 7, 2004
    #1
    1. Advertising

  2. Berlin Brown wrote:

    > This is basic java, and I have an idea, but without looking at the java
    > code, I cant get an 100% understanding.
    >
    > File _file = new File("somefilename");
    > FileWriter fileWriter = null;
    > BufferedWriter outputFile = null
    >
    > fileWriter = new FileWriter(_file);
    > outputFile = new BufferedWriter(fileWriter);
    > PrintWriter out = new PrintWriter(outputFile);
    >
    > outputFile.close(); // ???
    > out.close(); // ???
    >
    >
    > In this close what would physically close the file.
    > outputFile.close() ...which is the BufferedWriter or
    > out.close();


    Both, becaause all stream, writer and reader classes call the
    underlying stream/writer/reader's close() in their close()
    method. It is therefore sufficient (and advisable) to call the
    outermost stream/writer/reader's close().
     
    Michael Borgwardt, Sep 7, 2004
    #2
    1. Advertising

  3. "Michael Borgwardt" <> wrote in message
    news:...
    > Berlin Brown wrote:
    >
    >> This is basic java, and I have an idea, but without looking at the java
    >> code, I cant get an 100% understanding.
    >>
    >> File _file = new File("somefilename");
    >> FileWriter fileWriter = null;
    >> BufferedWriter outputFile = null
    >>
    >> fileWriter = new FileWriter(_file);
    >> outputFile = new BufferedWriter(fileWriter);
    >> PrintWriter out = new PrintWriter(outputFile);
    >>
    >> outputFile.close(); // ???
    >> out.close(); // ???
    >>
    >>
    >> In this close what would physically close the file.
    >> outputFile.close() ...which is the BufferedWriter or
    >> out.close();

    >
    > Both, becaause all stream, writer and reader classes call the
    > underlying stream/writer/reader's close() in their close()
    > method. It is therefore sufficient (and advisable) to call the
    > outermost stream/writer/reader's close().


    Conversely, if you're done with an output filter but don't want to close the
    underlying stream, just flush() the filter. (There's no similar solution
    with input filters, since there's no general way to push the stuff buffered
    in the filter back onto the input stream.)
     
    Mike Schilling, Sep 7, 2004
    #3
  4. Berlin  Brown

    Filip Larsen Guest

    Berlin Brown wrote

    > File _file = new File("somefilename");
    > FileWriter fileWriter = null;
    > BufferedWriter outputFile = null
    >
    > fileWriter = new FileWriter(_file);
    > outputFile = new BufferedWriter(fileWriter);
    > PrintWriter out = new PrintWriter(outputFile);
    >
    > outputFile.close(); // ???
    > out.close(); // ???
    >
    > In this close what would physically close the file.
    > outputFile.close() ...which is the BufferedWriter or
    > out.close();


    The typically use is to close the outermost Writer in the chain, which
    in your case is the BufferedWriter. It will then flush its buffers and
    propagate close to the next Writer in the chain. There is really no need
    to close the FileWriter explicitely, but if you do you should do it
    *after* you have closed or at least flushed the BufferedWriter.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Sep 7, 2004
    #4
  5. Filip Larsen wrote:

    > Berlin Brown wrote


    >>fileWriter = new FileWriter(_file);
    >>outputFile = new BufferedWriter(fileWriter);
    >>PrintWriter out = new PrintWriter(outputFile);
    >>
    >>outputFile.close(); // ???
    >>out.close(); // ???


    > The typically use is to close the outermost Writer in the chain, which
    > in your case is the BufferedWriter.


    Look a bit closer: in his case the outermost Writer is the PrintWriter.
    Just pointing it out in hopes of avoiding confusion.


    John Bollinger
     
    John C. Bollinger, Sep 8, 2004
    #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. Denon
    Replies:
    1
    Views:
    584
    Saravana [MVP]
    Nov 14, 2003
  2. Daniel Albisser
    Replies:
    1
    Views:
    1,084
    GaryM
    Apr 7, 2004
  3. Paul van Rossem
    Replies:
    0
    Views:
    608
    Paul van Rossem
    Apr 7, 2005
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    877
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Replies:
    0
    Views:
    509
Loading...

Share This Page