DataInputStream giving java.io.EOFException

為{幸葍}努か 提交于 2019-12-04 19:44:50

The method readUTF will never return null. Instead, you should do:

while(in.available()>0) {
    String value = in.readUTF();

Looking at javadocs, an EOFException is thrown if this input stream reaches the end before reading all the bytes.

Use FilterInputStream.available().

InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
String value;
while(in.available() > 0 && (value = in.readUTF()) != null) {
    System.out.println(value);
}

...

http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

try {
    while (true) {
        System.out.println(in.readUTF());
    }
    } catch (EOFException e) {
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!