java - 本地文件的 java.io.InputStream 何时会在关闭时抛出异常?

标签 java file-io exception

大家下午好。我知道如果我们 close()一个java.io.OutputStream , it may throw an IOException当有硬盘错误时。

至于java.io.InputStream , Javamex指出:

A normal closure of a network socket actually involves a closure request (TCP/IP FIN packet) being sent over the connection and waiting for the other end to acknowledge this closure request. (In fact, the other end of the connection then in turn sends a closure request, which the closing end acknowledges.) So in the case of a socket input stream, a closure operation actually involves sending traffic over the connection and the closure can thus fail with an error.

现在我想知道在什么情况下 java.io.InputStream 会(或子类)本地文件(网络访问不需要)曾经在我们调用close()时抛出异常?

最佳答案

这是一种情况:

  • 应用程序的另一部分错误地关闭了与 InputStream 关​​联的基础文件描述符。当 Java 运行时调用系统 close() 函数时,它可能会得到一个 EBADF 错误,表明文件句柄无效。

上述情况可能表明应用程序的其他地方存在严重错误。

关于java - 本地文件的 java.io.InputStream 何时会在关闭时抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8965266/

相关文章:

java - 记录时异常有什么好处?

java - dataSnapshot.getChildren() 获取用户 ID 但不获取值

java - 单击时图像不居中

java - 使用java将日期和时间戳插入数据库的最佳方法

c++ - 每行读取 int 并显示百分比

java - 从txt文件中读取整数并存储到数组中

c++ - C++ 抛出装饰有什么用吗?

java - Firebase 聊天应用程序 : query messages, 优化应为 OR 的查询,或改进 NoSQL 数据库结构

java - 多个线程可以同时将数据写入文件吗?

java - 当我们可以关闭文件并且所有内容都在 catch block 中时,finally block 需要什么