有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

输入JAVA的流中何时会发生EOFEException

我正在使用DataInputStream,对EOFEException有一个问题

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

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

如果outputstream连接到inputstream和outputstream。调用close()时,inputstream将接收EOFEException还是IOException

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.

outputstream上的关闭是否会在datainputstream端产生EOFEException或IOException


共 (5) 个答案

  1. # 1 楼答案

    EOFEException是IOException的一个子类。如果您试图从流中读取数据,但没有更多数据可读取(例如,因为您的DataInputStream被包装在FileInputStream中,并且您试图读取的字节数超过了文件中剩余的字节数),则会抛出该错误

  2. # 2 楼答案

    引发EOFEException:

    1. 如果流中没有数据,但您正在尝试读取。。。例如,链流的读取方法,如DataInputStream、ObjectInputStream、RandomAccessFile,如果试图从FileInputStream读取,但FileInputStream为空,则会抛出EOFEException
    2. 或者如果格式不匹配。。。例如,如果int存在,并且您正在使用DataInputStream的readFloat()
  3. # 3 楼答案

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

    • read()返回-1
    • readLine()返回空值
    • readXXX()用于任何其他X抛出{}

    这条流仍然是开放的,但是你应该停止阅读并关闭它

  4. # 4 楼答案

    关键词出人意料

    如果使用DataInputStream并读取一个4字节的整数,但流中只剩下3个字节,则会得到一个EOFEException

    但如果在流的末尾调用read(),则返回-1,没有异常

  5. # 5 楼答案

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