Yus*_*494 4 java io file eofexception
我不知道为什么会出现java.io.EOFException.我想从服务器获取二进制流后写一个文件.
这是我的代码
inputStream = new DataInputStream(new BufferedInputStream(connection.getInputStream()));
FileOutputStream fos = new FileOutputStream("D:/Apendo API resumable download.txt");
byte b = inputStream.readByte();
while(b != -1){
fos.write(b);
b = inputStream.readByte();
}
fos.close();
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at HttpRequestJSON.JSONRequest.sendRequest(JSONRequest.java:64)
at HttpRequestJSON.Main.main(Main.java:56)
Run Code Online (Sandbox Code Playgroud)
Evg*_*eev 10
它说,DataInputStream.readByte API并没有说它在EOS上返回-1
返回:此输入流的下一个字节作为带符号的8位字节.
抛出:EOFException - 如果此输入流已到达结尾.
它假设在使用DataInputStream.readByte时,我们知道流中剩余了多少字节.否则,我们可以使用EOFException作为EOS的指标.
BTW如果你使用read(),你将在没有EOFException的EOS上获得-1