Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Closing files

  1. #1

    Default Closing files

    I have opened up an input file using
    File input=new File("hello.txt")
    Then I have created a scanner object that reads data from the file.
    Scanner read_file=new Scanner (input)
    my question is when closing the files why can't I just do input.close() since that is also closing the file?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,778
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Closing files

    Because close() is not a valid File method. Scanner objects are closed; File objects are not.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Closing files

    Since input is a File object, take a look at the list of methods at File (Java Platform SE 7 ) - there isn't a close method for File.

    On the other hand, take a look at the list of methods for Scanner at Scanner (Java Platform SE 7 ). You'll find a close method there.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Closing files

    my question is when closing the files why can't I just do input.close() since that is also closing the file?
    first there is no close() method for java.io.File

    and if you think of it, do you really need to close that file?
    because there many instances when creating a File object,
    Example:
    is the file already exist? if not then there is no reason to close it right?
    is file a directory? if so why do you need to close it?
    in that case, what is the sense of closing it?

  5. #5

    Default Re: Closing files

    Got your point but what I have been reading from my book, it guided me to close all of your ouput and input files when I'm done reading and writing with them...I'm aware that whenever I don't close the output file, then nothing is displayed on my output file when im writing to it. Vice versa for input file, everything seems to be operating well even if I don't close the file. I will keep the close() method in mind though

  6. #6
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    462
    Thanks
    0
    Thanked 89 Times in 74 Posts

    Default Re: Closing files

    You do not open a File when creating an instance of the File class.
    The class File is merely information about a file. Like its name, size and other useful data.
    A file is opened when you create a reader / writer or some kind of stream that is using the file.
    If you want to close that file you have to close the reader / writer.

  7. The Following User Says Thank You to Cornix For This Useful Post:

    fahman_khan75@yahoo.com (March 20th, 2014)

  8. #7

    Default Re: Closing files

    Sweet makes much for sense!thank you Cornix!

Similar Threads

  1. how to restart applet without closing it
    By hwoarang69 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2013, 07:03 PM
  2. help with JButton closing a JFrame :)
    By skerridge in forum AWT / Java Swing
    Replies: 15
    Last Post: January 16th, 2012, 12:11 AM
  3. [SOLVED] Is This Closing To Code Normal?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 8th, 2012, 01:08 AM
  4. How to allow my program to keep runningwithout closing it?
    By esplanade56 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2011, 06:11 AM
  5. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM