Why no close() method for the File object ?

M

me2

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.
 
S

Simon Brooke

me2 ('[email protected]') said:
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 ?

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

--
(e-mail address removed) (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
 
M

Michael Rauscher

me2 said:
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
 
T

Thomas Hawtin

me2 said:
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
 
P

Patricia Shanahan

Michael said:
me2 said:
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.

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
 
M

Michael Rauscher

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

Right.

Michael
 
C

Chris Uppal

Thomas said:
BufferedOutputStream.close is broken.

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

-- chris
 
T

Thomas Hawtin

Chris said:
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
 
C

Chris Uppal

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top