是什么在Java中引发IOException? [英] What throws an IOException in Java?

查看:122
本文介绍了是什么在Java中引发IOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.io.IOException似乎是最常见的异常类型,巧合的是,它似乎也是最模棱两可的.

java.io.IOException seems to be the most common type of exception, and coincidentally, it seems to also be the most ambiguous.

每次使用套接字,文件等进行写操作时,我都会不断看到throws IOException.但是,实际上我从来没有触发过throws IOException,因此,我想知道触发异常的原因是什么.该文档对于解释发生的情况不是很有帮助:

I keep seeing the throws IOException whenever writing with sockets, files, etc. I've never actually had one fired on me, however, so I'm wondering what it is that is supposed to fire the exception. The documentation isn't very helpful in explaining what's going on:

表示发生了某种I/O异常.此类是由失败或中断的I/O操作产生的常规异常类别.

Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

在哪些情况下会引发IOException?应该如何使用它?

What are some instances where an IOException would be thrown, and how is it supposed to be used?

推荐答案

假设您是:

  1. 正在读取网络文件并断开连接.
  2. 读取不再可用的本地文件.
  3. 使用某些流读取数据,而使用其他一些进程关闭该流.
  4. 尝试读取/写入文件,但没有权限.
  5. 试图写入文件,但磁盘空间不再可用.

还有更多例子,但以我的经验来看,这是最常见的例子.

There are many more examples, but these are the most common, in my experience.

这篇关于是什么在Java中引发IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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