你有没有看到Java File close()抛出异常? [英] Have you ever seen a Java File close() throw an exception?

查看:483
本文介绍了你有没有看到Java File close()抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何可关闭对象调用close方法时,有没有人看到异常?

解决方案

flush 失败,那么IOException 将抛出关闭。可能的原因包括:




  • 文件系统已满,或用户超出配额,


  • 由于网络或其他问题,远程文件系统不可用,

  • 文件系统被强制卸载

  • (可能)如果通过 OutputStreamWriter 或类似文件写入文件,则会导致字符编码错误,

  • 如果文件是设备文件,则设备错误

  • 如果可关闭的是网络流,则丢失的连接

  • 如果

  • 等等。



我已经看到其中一些。其他的不太可能。



但是,如果您正在撰写的数据很重要,那么您应该允许关闭失败。例如,如果您的应用程序正在写出文件系统填满的关键文件,则应用程序在将文件的旧版本替换为截断版本之前,请更好地注意这一点。


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

解决方案

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.

这篇关于你有没有看到Java File close()抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆