Results 1 to 10 of 10
  1. #1
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default When do i use the close() method

    I've been having a bit of an issue with the close method. In my program at the very beginning i check for the existance of a file, and if it doesnt exist i create it with a FileWriter wrapped in a Bufferedwriter. example:

    Java Code:
    boolean fexists;
    if ((fexists = new File("registrar.txt").exists()) != true) {
            System.out.println("Registry not found....creating it");
            try {
            FileWriter fileOut = new FileWriter("registrar.txt");
            BufferedWriter fileBuff = new Bufferedwriter(fileOut);
            String registrarBanner = "----Registrar----\n";
            fileBuff.write(registrarBanner);
    
            fileBuff.close();
            //fileOut.close();
                 } catch (IOException e) {
                         e.printStackTrace();
                    }
    }

    In General i understand everything thats going on here ^ however if i uncomment the commented line //fileOut.close(); even though i do this after i use it, the file wont write and it will throw the IOException

    Can anyone maybe shed a bit of light on the issue, or point me in the right direction??
    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aadem View Post
    I've been having a bit of an issue with the close method. In my program at the very beginning i check for the existance of a file, and if it doesnt exist i create it with a FileWriter wrapped in a Bufferedwriter. example:

    Java Code:
    boolean fexists;
    if ((fexists = new File("registrar.txt").exists()) != true) {
            System.out.println("Registry not found....creating it");
            try {
            FileWriter fileOut = new FileWriter("registrar.txt");
            BufferedWriter fileBuff = new Bufferedwriter(fileOut);
            String registrarBanner = "----Registrar----\n";
            fileBuff.write(registrarBanner);
    
            fileBuff.close();
            //fileOut.close();
                 } catch (IOException e) {
                         e.printStackTrace();
                    }
    }

    In General i understand everything thats going on here ^ however if i uncomment the commented line //fileOut.close(); even though i do this after i use it, the file wont write and it will throw the IOException

    Can anyone maybe shed a bit of light on the issue, or point me in the right direction??
    Thanks!
    The general rule is: if you close a Writer (any type of Writer) that wraps another Writer, that other wrapped Writer will also be closed; so you just close the outermost Writer and all the wrapped Writers will be closed for you.

    Also, as an idiom, close anything in a finally block.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default ...

    i love you

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You only need to close the upper stream and it will close the underlying streams.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    And to follow good practice use a finally block:
    The finally Block (The Java™ Tutorials > Essential Classes > Exceptions)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Is my reply (#2) invisible?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I think aadem's "i love you" reply was directed at your reply, Jos. ;)

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Is my reply (#2) invisible?
    Nope, just read too quickly. S* happens.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    I think aadem's "i love you" reply was directed at your reply, Jos. ;)
    Eeeew!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Don't knock it, Jos.
    They might be rich!
    ;)

    Quote Originally Posted by aadem View Post
    the file wont write and it will throw the IOException
    It's this bit I don't understand, though.
    Surely the file will already be written since the stream will have been closed by the closing of the buffered writer?

    The IOException I understand since the stream's already closed, but the failure to write the file?
    Hopefully the OP is simply mistaken.

Similar Threads

  1. JMF mediaPlayer.close
    By Gog in forum New To Java
    Replies: 2
    Last Post: 02-09-2011, 07:22 PM
  2. funtion of close() method?
    By hacikho in forum New To Java
    Replies: 7
    Last Post: 10-11-2010, 02:08 AM
  3. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  4. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 07:01 PM
  5. close to zero
    By nokicky in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 05:30 PM

Posting Permissions

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