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