while循环中的ObjectInputStream readObject

while循环中的ObjectInputStream readObject,第1张

while循环中的ObjectInputStream readObject
while(Object obj = ois.readObject()) {  <-- Not Working//do something with object    }

当您说“不工作”时,由于编译器消息中所述的原因,您真正的意思是“不编译”:

Object
不是
boolean
表达式,也不能在
while
条件中声明变量。

但是该代码仍然无效。读取到任意流的末尾的正确方法

ObjectInputStream
是catch
EOFException
,例如,如下所示:

try{    for (;;)    {        Object object = in.readObject();        // ...    }}catch (SocketTimeoutException exc){    // you got the timeout}catch (EOFException exc){    // end of stream}catch (IOException exc){    // some other I/O error: print it, log it, etc.    exc.printStackTrace(); // for example}

请注意,在意见建议,以测试

readObject()
返回值
null
正确的。仅
null
当您写了时,它才会返回
null



欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5490567.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存