Closing IO Streams

Discussion in 'Java' started by ZOCOR, Aug 27, 2004.

  1. ZOCOR

    ZOCOR Guest

    Hi

    Consider:

    try
    {
    FileReader fr = new FileReader("file.txt");
    BufferedReader br = new BufferedReader(fr);
    doSomething();
    br.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    Questions:

    1. Do i also need to close FileReader fr by doing fr.close?
    2. if I want to parse a new file, do i just make fr point to a new object
    say fr = new FileReader("file2.txt")? or start over again?

    Thanks in advance

    ZOCOR





    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.737 / Virus Database: 491 - Release Date: 11/08/2004
     
    ZOCOR, Aug 27, 2004
    #1
    1. Advertising

  2. "ZOCOR" <> ha scritto nel messaggio
    news:%QDXc.9670$...
    > Hi
    >
    > Consider:
    >
    > try
    > {
    > FileReader fr = new FileReader("file.txt");
    > BufferedReader br = new BufferedReader(fr);
    > doSomething();
    > br.close();
    > }
    > catch (IOException e)
    > {
    > e.printStackTrace();
    > }
    >
    > Questions:
    >
    > 1. Do i also need to close FileReader fr by doing fr.close?


    No, the underlying stream is closea as well.

    > 2. if I want to parse a new file, do i just make fr point to a new object
    > say fr = new FileReader("file2.txt")? or start over again?


    No, you must start over again. The variable br holds a reference of the old
    FileReader.

    CarloP.
     
    Carlo Pellegrini, Aug 27, 2004
    #2
    1. Advertising

  3. ZOCOR

    ZOCOR Guest


    > > Hi
    > >
    > > Consider:
    > >
    > > try
    > > {
    > > FileReader fr = new FileReader("file.txt");
    > > BufferedReader br = new BufferedReader(fr);
    > > doSomething();
    > > br.close();
    > > }
    > > catch (IOException e)
    > > {
    > > e.printStackTrace();
    > > }
    > >
    > > Questions:
    > >
    > > 1. Do i also need to close FileReader fr by doing fr.close?

    >
    > No, the underlying stream is closea as well.
    >
    > > 2. if I want to parse a new file, do i just make fr point to a new

    object
    > > say fr = new FileReader("file2.txt")? or start over again?

    >
    > No, you must start over again. The variable br holds a reference of the

    old
    > FileReader.


    But the old FileReader now points to something else:

    fr = new FileReader("file2.txt");

    br holds a reference to fr and fr is now something different, so it should
    work right?


    ZOCOR




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.737 / Virus Database: 491 - Release Date: 11/08/2004
     
    ZOCOR, Aug 27, 2004
    #3
  4. ZOCOR wrote:
    >>>1. Do i also need to close FileReader fr by doing fr.close?

    >>
    >>No, the underlying stream is closea as well.


    However, the close() call should happen in a finally
    block after the catch, otherwise, the stream will
    remain open if an exception is thrown.

    >>>2. if I want to parse a new file, do i just make fr point to a new object
    >>>say fr = new FileReader("file2.txt")? or start over again?

    >>
    >>No, you must start over again. The variable br holds a reference of the old
    >>FileReader.

    >
    > But the old FileReader now points to something else:
    >
    > fr = new FileReader("file2.txt");


    No, it is the variable that has the name "fr" in your code
    that holds a reference which points at a new object.


    > br holds a reference to fr


    No, the object at which the reference in br points holds a copy of the value
    of fr at the time the br object was created, i.e. a reference to the object
    fr pointed at at that time. That reference does not change, no matter what
    you do.
     
    Michael Borgwardt, Aug 27, 2004
    #4
    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. Knute Johnson

    Closing sockets and streams?

    Knute Johnson, Jan 21, 2006, in forum: Java
    Replies:
    14
    Views:
    664
    Thomas Hawtin
    Jan 24, 2006
  2. Evgeni Sergeev
    Replies:
    2
    Views:
    453
    Jean Brouwers
    Dec 28, 2004
  3. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    678
    Thomas Hansen
    Feb 15, 2007
  4. Juha Nieminen

    Re: Closing file streams

    Juha Nieminen, Jan 27, 2009, in forum: C++
    Replies:
    3
    Views:
    540
    James Kanze
    Jan 29, 2009
  5. Philipp

    Closing decorated streams

    Philipp, Jul 9, 2009, in forum: Java
    Replies:
    6
    Views:
    335
    Knute Johnson
    Jul 11, 2009
Loading...

Share This Page