简体   繁体   English

为什么在Java中读取datainputstream时出现EOF异常?

[英]Why do I get EOF exception while reading a datainputstream in java?

Here is what I am trying to read : 这是我要阅读的内容:

   FileInputStream fis=new FileInputStream("count.txt");
   BufferedInputStream bis=new BufferedInputStream(fis);
   DataInputStream dis=new DataInputStream(bis);
   while(dis.read()!=-1)
   {
    orderCount=dis.readInt();
    newOrderCount=dis.readInt();
   }

Count.txt has data such as 0 0 0 0 0 0 0 1, but for some reason i get an EOF exception,please help! Count.txt具有数据,例如0 0 0 0 0 0 0 0 1,但是由于某种原因我收到了EOF异常,请帮助!

EOFException means you tried to read past the end of the stream, which probably happened because you are reading a byte and then throwing it away, which is already a bug, as it puts you out of sync with the sender. EOFException意味着您试图读取流的末尾,这可能是因为您正在读取一个字节然后将其丢弃,这已经是一个错误,因为它使您与发送方不同步,这可能是发生的。 Change the loop test to while (true) , catch EOFException , and, when you get it, close the socket and break out of the loop. 将循环测试更改为while (true) ,捕获EOFException ,并在获取测试时关闭套接字并退出循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果-1是标准EOF字节,为什么DataInputStream这样做? - If -1 is the standard EOF byte, why does DataInputStream do this? 为什么我在WEKA中读取该ARFF文件会导致EOF早熟? - Why do I get premature EOF reading this ARFF File in WEKA? 为什么在Java中收到EOF异常? - Why am I getting an EOF Exception in Java? 为什么在使用 readUTF 读取时出现 EOF 异常? - Why I am getting EOF exception when reading with readUTF? 为什么在Java中读取简单文本文件时在do while循环中的if语句中出现NullPointerException - Why do i get a NullPointerException at If statement in do while loop when reading a simple textfile in Java 为什么在Java中会出现NoSuchFieldError异常? - Why do I get a NoSuchFieldError exception in Java? 为什么在Java中会收到NoClassDefFoundError异常? - Why do I get a NoClassDefFoundError Exception in Java? 为什么在使用datainputstream,java,android时会得到0kb的文件? - Why I get a 0kb file when I use datainputstream, java, android? HttpsUrlConnection在GET请求时获取EOF异常 - HttpsUrlConnection getting EOF exception while GET request 在Java 11 HTTP / 2客户端中读取时无法获得流1:达到EOF - Can't get Stream 1: EOF reached while reading in Java 11 HTTP/2 Client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM