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

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

问题描述

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

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

根据java docs:

According to java docs:


表示在输入过程中意外地达到了
流的文件结尾或结尾

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.

输出流上的close是否会在datainputstream端产生EOFException或IOException?

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

推荐答案

关键字是意外。

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

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.

但是如果你在流的末尾调用read(),那么你只需返回-1并且没有例外。

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

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

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