JAVA 的流中什么时候会发生 EOFException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/664331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:36:24  来源:igfitidea点击:

When will an EOFException occur in JAVA's streams

javainputstreameofexception

提问by jbu

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

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

According to java docs:

根据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.

表示在输入期间意外到达文件结尾或流结尾。

此异常主要由数据输入流用于表示流结束。请注意,许多其他输入操作在流结束时返回一个特殊值,而不是抛出异常。

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?

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

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

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

An IOException is described as:

IOException 被描述为:

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.

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

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

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

回答by Pyrolistical

The key word is unexpected.

关键词出乎意料。

If you use DataInputStream and read a 4 byte integer but there were only 3 bytes remaining in the stream you'll get an EOFException.

如果您使用 DataInputStream 并读取一个 4 字节整数,但流中只剩下 3 个字节,您将收到 EOFException。

But if you call read() at the end of stream you'll just get -1 back and no exception.

但是如果你在流的末尾调用 read() 你只会得到 -1 并且没有例外。

回答by Simon Nickerson

EOFException is a subclass of IOException. It will be thrown if you attempt to read from the stream and there is no more data to be read (e.g. because your DataInputStream is wrapped around a FileInputStream and you're trying to read more bytes than are left in the file).

EOFException 是 IOException 的子类。如果您尝试从流中读取并且没有更多的数据要读取(例如,因为您的 DataInputStream 被包裹在 FileInputStream 周围并且您试图读取的字节数多于文件中剩余的字节数),它将被抛出。

回答by Dan Breslau

Answering another part of your question: Yes, EOF means that no more data will be seen on the stream; you should close it.

回答您问题的另一部分:是的,EOF 意味着在流中不会看到更多数据;你应该关闭它。

回答by Saili Waichal

EOFException is thrown:

抛出 EOFException:

  1. if there is no data in a STREAMbut you are trying to read...eg read methods of chain streams like DataInputStream, ObjectInputStream, RandomAccessFile throw EOFException if they are trying to read from FileInputStream but the FileInputStream is empty
  2. or if the formats are not matching...eg if int is present and you are using readFloat() of DataInputStream
  1. 如果STREAM 中没有数据但您正在尝试读取...例如,如果链流的读取方法如 DataInputStream、ObjectInputStream、RandomAccessFile 尝试从 FileInputStream 读取但 FileInputStream 为空,则会抛出 EOFException
  2. 或者如果格式不匹配...例如,如果存在 int 并且您正在使用 DataInputStream 的 readFloat()

回答by user207421

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

当您到达流的末尾(文件末尾,或对等方关闭连接)时:

  • read()returns -1
  • readLine()returns null
  • readXXX()for any other X throws EOFException.
  • read()返回 -1
  • readLine()返回空值
  • readXXX()对于任何其他 X 投掷EOFException

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

流仍然打开,但您应该停止读取并关闭它。