java a.close,Have you ever seen a Java File close() throw an exception?

问题

Has anyone ever seen an exception thrown when calling close method on any closable object?

回答1:

An IOException will be thrown on close if the final flush fails. Possible causes include:

the file system is full, or the user is over quota,

hard disc errors,

a file system was forcibly unmounted,

a remote file system is unavailable due to networking or other problems,

(possibly) a character encoding error if writing to the file via an OutputStreamWriter or similar,

a device error if the "file" is a device file,

a lost connection if the closeable is a network stream,

a broken pipe if the closeable is a pipe to external process,

and so on.

I have certainly seen some of these. Others are unlikely.

However, if the data you are writing is important then you should allow for close failing. For example, if your application is writing out a critical file the file system fills up, your application had better notice this before it replaces the old copy of the file with the truncated version.

回答2:

Yes, it's not that rare, IMHO if you are working with anything other than non-local disk files.

Close() works if at that point your closable is still valid and open. Many things like pipes, remote files, etc., can die prematurely.

In addition, I have seen code that ignores errors on open and write and still tries to close (e.g., in a finally block).

回答3:

I haven't, but it's possible. Imagine if there's an OutputStream that for some reason hasn't written to the file yet. Well, calling close() will flush out the data, but if the file is locked - then an IOException would be raised.

回答4:

Not in terms of file-io, but in terms of sockets the close will raise IOException when the other side has aborted the connection. For example, when you fire a HTTP request on a (large) webpage and then immediately navigate away by clicking another link on the webpage (while it isn't finished loading), then the server side will get an IOException (or a subclass like ClientAbortException in Tomcat servers and clones) when the outputstream of the HTTP response is to be flushed/closed.

回答5:

Old post and long since answered but here's a real example:

The following code will except out when bufferedWriter.close() is called. This happens because the BufferedWriter's underlying Writer (the FileWriter) has already been closed and when a BufferedWriter closes, it first attempts to flush any data in its buffer to its underlying Writer.

File newFile = new File("newFile.txt");

FileWriter fileWriter = new FileWriter(newFile);

BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

bufferedWriter.write("Hello World");

fileWriter.close();

bufferedWriter.close();

Note: If there's no data in the buffer [comment out the write() line or add a flush() call] then no exception will be generated

回答6:

I guess you could try to force this by unplugging the disk your file is on. But on any Closable? I think it would be easy to get something that uses a socket to throw an exception upon closing.

回答7:

Try yanking a USB drive with an open file on it. If it doesn't give an exception I'd be pretty surprised.

回答8:

I have - in my unit tests against mocks ;)

来源:https://stackoverflow.com/questions/2765704/have-you-ever-seen-a-java-file-close-throw-an-exception

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值