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

查看:41
本文介绍了什么会在 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.然而,我实际上从未对我开火,所以我想知道它应该是什么触发异常.该文档对解释正在发生的事情不是很有帮助:

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天全站免登陆