JAVA 的流中什么时候会发生 EOFException [英] When will an EOFException occur in JAVA's streams

查看:30
本文介绍了JAVA 的流中什么时候会发生 EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataInputStream 并且有一个关于 EOFExceptions 的问题.

I am working with a DataInputStream and had a question about EOFExceptions.

根据java文档:

表示文件结束或结束流已意外到达输入时.

Signals that an end of file or end of stream has been reached unexpectedly during input.

这个异常主要被数据使用输入流以表示流结束.请注意,许多其他输入操作在结束时返回一个特殊值流而不是抛出例外.

This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.

这是否意味着当生成 EOFException 时,流将永远不会再次打开?这是否意味着您永远不应该期望从中获得更多数据?

Does this mean that when a EOFException is generated, the stream will not NEVER open again? Does it mean you should NEVER expect any more data from it ever?

如果输出流连接到输入流并调用 outputstream.close(),输入流会收到 EOFException 还是 IOException?

If an outputstream is connected to an inputstream and outputstream.close() is called, will an inputstream receive the EOFException or an IOException?

一个 IOException 被描述为:

An IOException is described as:

表示某些 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.

输出流上的关闭是否会在数据输入流端产生 EOFException 或 IOException?

Does a close on the outputstream produce either a EOFException or an IOException on the datainputstream side?

推荐答案

当你到达一个流的结尾(文件结尾,或者对等点关闭连接):

When you reach the end of a stream (end of file, or peer closes the connection):

  • read() 返回 -1
  • readLine() 返回 null
  • readXXX() 任何其他 X 抛出 EOFException.
  • read() returns -1
  • readLine() returns null
  • readXXX() for any other X throws EOFException.

该流仍处于打开状态,但您应该停止读取并关闭它.

The stream is still open, but you should stop reading from it and close it.

这篇关于JAVA 的流中什么时候会发生 EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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