Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Why no close() method for the File object ?

Thread view: 
me2 - 08 Nov 2006 18:03 GMT
I am using the File object in an app.

I noticed there is no close() method for it.  Why not ?  What am I missing ?

Does Java just rely on the object going out of scope to close the file ?
Or does one only have to close the stream associated with the file ?

Thanks.
Simon Brooke - 08 Nov 2006 18:10 GMT
> I am using the File object in an app.
>
[quoted text clipped - 3 lines]
> Does Java just rely on the object going out of scope to close the file ?
> Or does one only have to close the stream associated with the file ?

You don't close the file, you close the stream.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       ;; Perl ... is the Brittney Spears of programming - easily accessible
       ;; but, in the final analysis, empty of any significant thought
                               ;; Frank Adrian on Slashdot, 21st July 2003

Michael Rauscher - 08 Nov 2006 18:20 GMT
me2 schrieb:
> I am using the File object in an app.
>
> I noticed there is no close() method for it.  Why not ?  What am I missing ?

You can't open a File object - so you can't close it.

A File object is just "an abstract representation of file and directory
pathnames" [API].

> Does Java just rely on the object going out of scope to close the file ?
> Or does one only have to close the stream associated with the file ?

There's no stream associated with a File object. But you can open
streams by passing File objeocts to their cnstructors or methods
respecutively. And, yes, yo've to close the stream.

Bye
Michael
Patricia Shanahan - 08 Nov 2006 18:31 GMT
> me2 schrieb:
>> I am using the File object in an app.
[quoted text clipped - 13 lines]
> streams by passing File objeocts to their cnstructors or methods
> respecutively. And, yes, yo've to close the stream.

I think some confusion could have been avoided if it had been called
"java.io.PathName" instead of "java.io.File".

For example, the exists() method makes sense as an instance method,
because there may not be any file matching the path name. The lack of
open and close methods also makes more sense when you think of it as a
path name rather than a file.

Patricia
Michael Rauscher - 08 Nov 2006 18:44 GMT
Patricia Shanahan schrieb:
> I think some confusion could have been avoided if it had been called
> "java.io.PathName" instead of "java.io.File".

Right.

Michael
Thomas Hawtin - 08 Nov 2006 18:21 GMT
> I am using the File object in an app.
>
> I noticed there is no close() method for it.  Why not ?  What am I missing ?

It represents a file (really a file path). It does not represent an open
file channel.

> Does Java just rely on the object going out of scope to close the file ?
> Or does one only have to close the stream associated with the file ?

No. If you have a file *stream* then you make sure you always close it:

public void write(File file) throws IOException {
    OutputStream rawOut = new FileOutputStream(file);
    try {
        BufferedOutputStream out = new BufferedOutputStream(rawOut);
        out.write(42);
        ...
        out.flush();
    } finally {
        rawOut.close();
    }
}

Note, it's important to flush the buffered output in the happy case.
BufferedOutputStream.close is broken.

Tom Hawtin
Chris Uppal - 08 Nov 2006 18:56 GMT
> BufferedOutputStream.close is broken.

It is ?  How, and since when ?  The version in 1.5 (actually inherited from
FilterOutputStream) looks OK to me.

   -- chris
Thomas Hawtin - 08 Nov 2006 19:39 GMT
>> BufferedOutputStream.close is broken.
>
> It is ?  How, and since when ?  The version in 1.5 (actually inherited from
> FilterOutputStream) looks OK to me.

Ignoring an exception is almost always a mistake.

    public void close() throws IOException {
        try {
          flush();
        } catch (IOException ignored) {
        }
        out.close();
    }

Suppose flush causes your file to exceed the disk capacity. An exception
is thrown, but ignored. The stream is the closed normally and no
exception is thrown.

A better implementation would be:

    public void close() throws IOException {
        try {
            flush();
        } finally  {
            out.close();
        }
    }

Tom Hawtin
Thomas Hawtin - 08 Nov 2006 20:00 GMT
>>> BufferedOutputStream.close is broken.
>>
>> It is ?  How, and since when ?  The version in 1.5 (actually inherited
>> from
>> FilterOutputStream) looks OK to me.

> A better implementation would be:

See Bug 6390383

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6390383

(It' me with the first comment. I don't think the tickbox survives
indirection through the Sun Online thingy login page.)

Tom Hawtin
Chris Uppal - 09 Nov 2006 12:22 GMT
> > > BufferedOutputStream.close is broken.
> >
> > It is ?  How, and since when ?  The version in 1.5 (actually inherited
> > from FilterOutputStream) looks OK to me.
>
> Ignoring an exception is almost always a mistake. [...]

Ah, I see what you mean -- I was expecting to see broken buffering code.  I
agree that the empty exception handler is just silly.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.